Hello Daniweb,
I just created a small program that generates a random integer between a given range then asks the user to guess the number, giving hints like "Too high" or "Too low" along the way until they guess the correct number. I have it all working smoothly, except when the user finishes, they're asked if they wanna play again. And I have that in this method:

private static void playAgain() {

        System.out.print("\nPlay again? (Y/N)");
        String userResponse = userInput.next().toUpperCase();
        char userContinue = userResponse.charAt(0);

        if (userContinue == 'Y') {
            // User wants to play again, start over
        } else if (userContinue == 'N') {
            // User doesn't want to play again.  Display a goodbye message
            System.out.println("Thanks for playing. Goodbye!");
        } else {
            // If any invalid characters are entered, repeat the play again question
            System.out.println("Invalid response.  Please try again");


Now correct me if I'm wrong, but I think by using playAgain() within itself, I'm stacking the same method on top of the other one am I not? Alternatives to something like this?

Recommended Answers

All 2 Replies

The technique you are using looks like a recursive call. Eventually the program will run out of memory. Not likely when it requires user input for each loop.
A way to avoid recursive calls would be to use a loop that uses the user's input to control it looping back to start a new game.

yes, you are stacking up. you should have your while and your call to playAgain(); in the place(method) where you originally call playAgain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.