I am trying to write a void recursive method that receives a string message and int of how many times to output the message. I'm fairly new to programming, especially recursion, and cannot seem to find an answer to how to do this in a void method.
1 Answer
You can pass the number of time the string needs to be printed into recursive method decrementing it on each call:
public static void main(String[] args) {
printString("Hello World!", 6);
}
public static void printString(String message, int repeat) {
if (repeat == 0) {
return;
}
System.out.println(message);
printString(message, repeat - 1);
}