0

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
  • void return type does not matter. So if the method is something like: foo(String message, int count), decrement the count in the recursive call and stop when count is 0. Commented Apr 25, 2020 at 1:06

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.