+ 1

Recursion in java

I'm trying to accept an integer and a character. It should print out the integer that was entered and the character times the integer that was entered. Eg. if 3 and c was entered the output should be: 3 ccc. Thanks for any help. import java.util.Scanner; public class Histogram{ public static void Histogram_Recursive(int n, String c) { if(n==0) { return; } else { return n * Histogram_Recursive(n-1, c); } } public static void main(String[] args) { Scanner obj = new Scanner(System.in); System.out.println("Enter a Number: "); int number = obj.nextInt(); System.out.println("Enter a Character: "); String character = obj.next(); System.out.print("\n"); System.out.print(number +" "); Histogram_Recursive(number, character); } }

9th Feb 2020, 5:04 PM
Taylor
Taylor - avatar
5 Respostas
9th Feb 2020, 6:26 PM
Kevin ★
+ 5
If you want to return a value you should not use void. public static int histogram... But if you want to use void then you could add a print statement At the moment I am not sure how your method works. Can you give an example with input (n & c) and the expected output?
9th Feb 2020, 5:47 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Yes, you are welcome Taylor but... You can ask If you don't understand something in my code.
9th Feb 2020, 8:48 PM
Kevin ★
+ 1
Taylor You also need to do something with the string in your recursive method.
9th Feb 2020, 6:26 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thanks Kevin Star. That's exactly what I was looking for.
9th Feb 2020, 8:27 PM
Taylor
Taylor - avatar