Well, as I look to your looping logic it could be maintained and working as charm, you just need to move the initialization steps from the outside, to the inside do-while loop, in the beginning.

That way, at each start of the game, everything will work.

Also you're forgetting to go to next line before reading the user choice of yes/no, as you were reading just ints, at the end your a line ahead of the line containing the verdict yes/no

I tried this code in an online compiler, and it's working :

import java.util.Scanner;
import java.util.Random;

public class Main{

     public static void main(String []args){

        Random random = new Random();
        Scanner input = new Scanner(System.in);
        final int MAX = 100;

        System.out.println("Guess a number between 1 and " + MAX + ": ");
        String cont = new String();
        do {
            System.out.println("Let's play");
            //Initializations here
            int answer = random.nextInt(MAX) + 1;
            int guess = -1;
            int numberOfGuesses = 0;


            while (guess != answer) {
                numberOfGuesses++;
                guess = input.nextInt();

                if (guess == 101) {
                    break;
                } else if (guess > answer) {
                    System.out.println("Too High");
                    System.out.println("\nGuess again ");
                } else if (guess < answer) {
                    System.out.println("Too Low");
                    System.out.println("\nGuess again: ");
                } else {
                    System.out.println("Correct! The number was " + answer + ".");
                    System.out.println("It took you " + numberOfGuesses + " guesses.");
                }
            }

            System.out.println("\nWould you like to play again (yes/no)?");
            input.nextLine();//You have to go to the next line...
            cont = input.nextLine();
        } while (cont.equals("yes"));
        System.out.println("\nThanks for playing !");
        input.close();
     }
}
Answer from Lahcen YAMOUN on Stack Overflow
Top answer
1 of 2
1

Well, as I look to your looping logic it could be maintained and working as charm, you just need to move the initialization steps from the outside, to the inside do-while loop, in the beginning.

That way, at each start of the game, everything will work.

Also you're forgetting to go to next line before reading the user choice of yes/no, as you were reading just ints, at the end your a line ahead of the line containing the verdict yes/no

I tried this code in an online compiler, and it's working :

import java.util.Scanner;
import java.util.Random;

public class Main{

     public static void main(String []args){

        Random random = new Random();
        Scanner input = new Scanner(System.in);
        final int MAX = 100;

        System.out.println("Guess a number between 1 and " + MAX + ": ");
        String cont = new String();
        do {
            System.out.println("Let's play");
            //Initializations here
            int answer = random.nextInt(MAX) + 1;
            int guess = -1;
            int numberOfGuesses = 0;


            while (guess != answer) {
                numberOfGuesses++;
                guess = input.nextInt();

                if (guess == 101) {
                    break;
                } else if (guess > answer) {
                    System.out.println("Too High");
                    System.out.println("\nGuess again ");
                } else if (guess < answer) {
                    System.out.println("Too Low");
                    System.out.println("\nGuess again: ");
                } else {
                    System.out.println("Correct! The number was " + answer + ".");
                    System.out.println("It took you " + numberOfGuesses + " guesses.");
                }
            }

            System.out.println("\nWould you like to play again (yes/no)?");
            input.nextLine();//You have to go to the next line...
            cont = input.nextLine();
        } while (cont.equals("yes"));
        System.out.println("\nThanks for playing !");
        input.close();
     }
}
2 of 2
0

I would organize this into a class with methods first. This breaks down each piece of logic and makes it easier to read.

  1. Start a "play" loop
  2. Inside the "play" loop, have a "guess" loop
  3. Store the "continue" state in a known constant
  4. Only check if they are correct after they made the max number of guesses.
  5. Seed the Random object with the current system time

Also, I added a DEBUG flag to display the answer.

Update: The program is ignoring or not even asking for your "yes" input, because you were previously asking it for integers. You will need to clear/flush/reset the scanner's buffer by calling nextLine. Switching from integer to string leaves some data in the buffer. So your program will use the left-over character data in the buffer before it prompts you for your input.

import java.util.Scanner;
import java.util.Random;

public class HiLo implements Runnable {
    private static boolean DEBUG = true; // Disable to not show the answer...
    private static final int MAX_NUMBER = 100;
    private static final int MAX_GUESSES = 5;
    private static final String YES = "YES";

    private final Random rand;

    public HiLo() {
        this.rand = new Random(System.currentTimeMillis());
    }

    public static void main(String[] args) {
        new Thread(new HiLo()).start();
    }

    @Override
    public void run() {
        Scanner scan = new Scanner(System.in);
        String cont = "";
        do {
            play(scan);
            System.out.print("\nWould you like to play again (yes/no)? ");
            cont = scan.nextLine();
            System.out.println();
        } while (cont.equalsIgnoreCase(YES));
        scan.close();
    }

    public void play(Scanner scan) {
        boolean correct = false;
        int numberOfGuesses = 0;
        int answer = rand.nextInt(MAX_NUMBER) + 1;

        if (DEBUG) System.out.printf("The answer is: %d%n", answer);

        System.out.printf("Guess a number between 1 and %d%n", MAX_NUMBER);

        while (numberOfGuesses < MAX_GUESSES && !correct) {
            numberOfGuesses++;
            correct = makeGuess(scan, answer, numberOfGuesses);
        }

        if (numberOfGuesses <= MAX_GUESSES && correct) {
            System.out.printf("Correct! The number was %d. It took you %d guesses.%n", answer, numberOfGuesses);
        } else {
            System.out.printf("Sorry, but you did not guess: %d", answer);
        }
        scan.nextLine(); // Reset...
    }

    public boolean makeGuess(Scanner scan, int answer, int currentGuesses) {
        System.out.print("Guess: ");
        int guess = scan.nextInt();

        if (guess == answer) {
            return true;
        }

        if (currentGuesses < MAX_GUESSES) {
            if (guess > answer) {
                System.out.print("Too High, ");
            } else {
                System.out.print("Too Low, ");
            }
        }

        return false;
    }
}

Sample Output

The answer is: 64
Guess a number between 1 and 100
Guess: 50
Too Low, Guess: 75
Too High, Guess: 60
Too Low, Guess: 65
Too High, Guess: 64
Correct! The number was 64. It took you 5 guesses.

Would you like to play again (yes/no)? yes

The answer is: 88
Guess a number between 1 and 100
Guess: 50
Too Low, Guess: 90
Too High, Guess: 80
Too Low, Guess: 85
Too Low, Guess: 87
Sorry, but you did not guess: 88

Would you like to play again (yes/no)? no
🌐
Coderanch
coderanch.com › t › 534456 › java › Play-function
Play again function help? (Beginning Java forum at Coderanch)
Y or N?" twice... and I'm allowed to enter an input but it just loops through "play again? y or n?" instead of actually starting the program again. What am I doing wrong? ... I was surprised to see the second 'do' loop. With each prompt in the main do loop, you've already instructed the user to enter -1 if he/she wants to quit. That by itself could terminate the game. Then, your second do loop - just as you said - continues to loop, asking the player if they want to continue to play. That's all the loop does. There's no way for a true answer to get the logic back to the main do loop.
🌐
Chegg
chegg.com › homework-help › questions-and-answers › working-hi-lo-guessing-game-java-far-need-limit-3-attempts-guessing-however-problem-guess--q6768113
Solved: I Am Working On A Hi-lo Guessing Game In Java. Thi... | Chegg.com
(yes/no): "); String input = scan.nextLine(); if (input.equalsIgnoreCase("no")) playAgain = false; else if (input.equalsIgnoreCase("yes")) playAgain = true; playerWon = false; } System.out.println("You won " + gamesWon + " games."); } } ... ...
🌐
Coderanch
coderanch.com › t › 677789 › java › Play
Play again help (Beginning Java forum at Coderanch)
March 28, 2017 - Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH ... Boost this thread! ... I keep having a issue with variables and methods. ... New Help on letting the user to choose to play again or not.
🌐
Stack Overflow
stackoverflow.com › questions › 50258299 › program-finishes-if-user-does-not-select-yes-no-to-continue-java
Program finishes if user does not select yes/no to continue - java - Stack Overflow
public static void main(String[] args) throws FileNotFoundException { File file = new File("C:\\Users\\mmkp1\\Documents\\listofmovies.txt"); int attempts = 10; Scanner scanner = new Scanner(System.in); int weArePlaying = 1; while (weArePlaying == 1) { ... // This is in the while loop and I think this is why it end abruptly // as the value for areWePlaying does not equal 1 if we don't select // 1 or 2 if (weArePlaying == 2) { System.out.println("Do you want to play again? (yes/no)"); String anotherGame = scanner.nextLine(); if (anotherGame.equals("no")) { weArePlaying = 0; } else if (anotherGame.equals("yes")) { weArePlaying = 1; attempts = 10; guessedLetters.clear(); } else { weArePlaying = 2; System.out.println("Do you want to play again?
🌐
Stack Overflow
stackoverflow.com › questions › 43193910 › implementing-a-play-again-boolean
java - Implementing a play again boolean - Stack Overflow
Try again: "); guess = keyboard.nextInt(); count++; } } boolean isValidAnswer; System.out.print("\nWould you like to play again (yes/no)? "); playAgain = keyboard.next().toUpperCase(); isValidAnswer= playAgain.equals("YES") || playAgain.equals("NO"); if(! isValidAnswer) { System.out.println("Error: Please enter yes or no"); System.out.println(); } } while(playAgain.equals("YES")); //If user wins //Count = 1 word is guess if (guess == number) { if (count == 1) { round++; System.out.println("You got it in " + count + " guess."); ++win; } } java ·
Find elsewhere
🌐
YouTube
youtube.com › safdar dogar
How User Ask if They Want to Run Program Again in JAVA|How to Add Prompt Do You Want To Continue - YouTube
In This Video We Learn How User Ask if They Want to Run Program Again in JAVA|How to Add Prompt Do You Want To Continue Again and AgainWithSafdar Dogar
Published   May 9, 2020
Views   14K
🌐
Coderanch
coderanch.com › t › 547104 › java › letting-user-choose-play
New Help on letting the user to choose to play again or not. here is what i have (Beginning Java forum at Coderanch)
July 27, 2011 - I have it in a Do while loop, and I have the code to repeat again if the user selects yes. but it is not working. else if (guess == target) System.out.println("you are correct! do you want to play again?
🌐
Java-forums
java-forums.org › new-java › 68983-continue-yes-no-problem.html
"Continue Yes or No" Problem
February 17, 2013 - Variables only change if you assign them a new value. It has no concept of the user typing "yes" or "no" unless you tell it what to do with the input. Regards. ... Wouldn't a yes turn play in true ('play = true') and redo the do-while?
Top answer
1 of 3
3

I noticed that you had this piece of code repeated four times throughout your program.

  System.out.println("You win!");
  System.out.println("It took you " + tries + " tries.");
  System.out.println("Do you want to start again?");
  System.out.println("Type 1 to start again, type 2 to quit.");

One suggestion I would make is to insert this code into a method, that will you will only need to call the method and reduce the amount of code you have. For example you can do this:

public static void winningMessage(){

      System.out.println("You win!");
      System.out.println("It took you " + tries + " tries.");
      System.out.println("Do you want to start again?");
      System.out.println("Type 1 to start again, type 2 to quit.");
}

Then in your code just call the method, winningMessage();

Another suggestion I would make to your code is to only use one Scanner object. Within your code you have more than one when you only need one. You do not need multiple scanners for each time you are going to take user input you can continue to reuse the same scanner object.

Last suggestion I would make is to declare your variables up at the top of the program.

2 of 3
1

You have loaded all your code into the main() method. That is generally not good. You should aim to keep main() relatively small and have the workings hidden in other methods that you call from main(). Use main() more for flow control than for actually playing the game. Something like:

import java.util.Random;
import java.util.Scanner;

public class GuessingGameTest {

  // Shared class utilities.
  Random myRand = new Random();
  Scanner inScan = new Scanner(System.in);

  public static void main(String[] args) {

    // Introduce the game.
    System.out.println("Hello and welcome to my number guessing game.");

    // Play the game.
    boolean playing = true;
    while (playing) {

      // Play one game.
      playGame();

      // Another game?
      char reply = 'X';
      while (reply != '1' && reply != '2')
        System.out.println("Do you want to start again?");
        System.out.println("Type 1 to start again, type 2 to quit.");
        reply = inScan.nextChar();
      }

      if (reply == '2') {
        playing = false;  // Exit game.
      }
    }

    System.out.println("Thank you for playing.  Goodbye.");

  }

  static void playGame() {
    System.out.println("Pick a number: ");
    // Much code here.
  }

} // end class GuessingGameTest.

I have not compiled or tested this code, so test it thoroughly before using it.

Your use of exit() is not good practice either. Let the code finish naturally, and reserve exit() for exceptional situations, not for a normal termination.

🌐
CopyProgramming
copyprogramming.com › howto › how-do-i-ask-the-user-to-play-again-and-rerun-the-do-while-loop-with-a-simple-yes-or-no-question
Java: How do I ask the user to "play again" and rerun the do while loop with a simple yes or no question?
September 2, 2022 - Would you like to play again (yes/no)? yes The answer is: 88 Guess a number between 1 and 100 Guess: 50 Too Low, Guess: 90 Too High, Guess: 80 Too Low, Guess: 85 Too Low, Guess: 87 Sorry, but you did not guess: 88 Would you like to play again (yes/no)? no · Problem producing a new random number at the end of HiLo, I'm creating a HiLo guessing game in Java.
🌐
Stack Overflow
stackoverflow.com › questions › 54387857 › how-to-add-reset-game-with-yes-or-no-game-java
How to add reset game with yes or no ? Game Java - Stack Overflow
January 27, 2019 - import java.util.Scanner; public ... / NO"); answer= in.nextLine(); } while (answer.equalsIgnoreCase("YES")); if (answer.equalsIgnoreCase("NO")){ ......
🌐
Experts Exchange
experts-exchange.com › questions › 24304228 › How-to-restart-Java-GUI-application.html
Solved: How to restart Java GUI application? | Experts Exchange
April 8, 2009 - while(a){ //game code Object[] x = {"Yes","No"}; int y = JOptionPane.showOptionDialog(null, "Would you like to play again?", "My Game", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, x, x[0]); if(y == 1) a = false; } JOptionPane.showMessageDialog("Thank you for playing!");
Top answer
1 of 2
2

The cleanest way is to put all of the code in main in a function. Then call that function in a loop. Like this:

Scanner scanner = new Scanner(System.in);

    while (true) {
        runGame(scanner);
        // Game has finished

        System.out.println("Do you want to play again, type (5) for yes or (10) for no");

        if (scanner.nextLine().equals("10")) {
            break;
        }
    }

The scanner is passed to the function as the same one can be used there. There's no reason to have multiple scanners in your program, and it should be avoided.

2 of 2
0

Build a while (or do-while, which would be even a better choice) around it. Ask if it should be played again, if yes the condition of the while stays true, otherwise change it. Of course you have to initialize numSticks, numToTake and score to 0 again.

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Sticks3 {

    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int numSticks = 21;
        System.out.println("Would You Like to go first? (Yes/No)");
        Scanner input = new Scanner(System.in);
        String goFirst = input.nextLine();
        Scanner take = new Scanner(System.in);
        int numToTake = 0;
        int score = 0;

        boolean playAgain = true;
        Scanner askPlayAgainScanner = new Scanner (System.in);

        while (playAgain == true) {

            numSticks = 21;
            numToTake = 0;
            score = 0;

            while (numSticks > 0) {

                if (goFirst.equals("Yes") || goFirst.equals("yes")) {
                    System.out.println("There are " + numSticks + " sticks ");
                    System.out.println("How many sticks do you want to take (1 or 2)");
                    numToTake = take.nextInt();

                    if (numToTake > 2) {
                        numToTake = 2;

                    }

                    else if (numToTake < 1) {
                        numToTake = 1;
                    }
                    numSticks = numSticks - numToTake;

                    if (numSticks <= 0) {
                        System.out.println("You lose");
                        System.out.println("Your score is " + score);
                    } else {

                        if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
                            numToTake = 1;
                        } else {
                            numToTake = 2;
                        }
                        System.out.println("Computer takes " + numToTake + " sticks ");
                        numSticks = numSticks - numToTake;

                        if (numSticks <= 0) {
                            System.out.println(" You win ");
                            score++;
                            System.out.println("Your score is " + score);
                        }
                    }

                } else {
                    if ((numSticks - 2) % 3 == 0 || numSticks - 2 == 0) {
                        numToTake = 1;
                    } else {
                        numToTake = 2;
                    }
                    System.out.println("Computer takes" + numToTake + " sticks ");
                    numSticks = numSticks - numToTake;

                    if (numSticks <= 0) {
                        System.out.println("You win");
                        score++;
                        System.out.println("Your score is " + score);
                    } else {
                        System.out.println("There are " + numSticks + " sticks ");
                        System.out.println("How many sticks do you want to take (1 or 2)");
                        numToTake = take.nextInt();

                        if (numToTake > 2) {
                            numToTake = 2;
                        } else if (numToTake < 1) {
                            numToTake = 1;
                        }
                        numSticks = numSticks - numToTake;

                        if (numSticks <= 0) {
                            System.out.println("You win");
                            score++;
                            System.out.println("Your score is " + score);

                        }



                    }

                }
            }
            System.out.print("Do you want to play again? true for yes, false for no: ");
            boolean askPlayAgain = askPlayAgainScanner.nextBoolean();
        }
    }
}
🌐
BeginwithJava
beginwithjava.com › java › loops › do-while-loop.html
Do While Loop in Java
Enter integer: 6 Enter Y for yes or N for no: y Enter integer: 8 Enter Y for yes or N for no: y Enter integer: 4 Enter Y for yes or N for no: n Sum of the integers: 18 · Following example uses a do while loop to implement the Guessing the Number game. The program gives as many tries as the user needs to guess the number. import java.util.Scanner; // needed for Scanner Class /** * This program implements number guessing game.
Top answer
1 of 2
1

You already found a good position for adding this functionality:

System.out.println("Do you want to play again?: ");

The first step now is to also tell the user what he/she should enter after that question:

System.out.println("Do you want to play again? (enter 0 for yes and 1 for no): ");

After that we need to get the user input of course:

int number;
//If the user enters e.g. a string instead of a number, the InputMismatchException
//will be thrown and the catch-block will be executed
try {
    number = input.nextInt();
    
    //If number < 0 OR number > 1
    if(number < 0 || number > 1) {
        //The rest of the try-block will not be executed.
        //Instead, the following catch-block will be executed.
        throw new InputMismatchException();
    }
    break;  
}

catch(InputMismatchException e) {
    System.out.println("Enter 0=yes or 1=no");
    //Clears the scanner to wait for the next number
    //This is needed if the user enters a string instead of a number
    input.nextLine();
}

If you don't know about try-catch-statements yet, I suggest to read this explanation. For details about the InputMismatchException, please see the documentation.

The problem now is that the user only has one chance to enter 0 or 1. If the user makes a wrong input the program will just stop. One solution to this problem is to just put the code in a while-loop:

int number;
while(true) {
    try {
        number = input.nextInt();
        if(number < 0 || number > 1) {
            throw new InputMismatchException();
        }
        break;  
    }

    catch(InputMismatchException e) {
        System.out.println("Enter 0=yes or 1=no");
        input.nextLine();
    }
}

After this block, we can be sure that number is either 0 or 1. So now we can add a simple if-statement to check the value:

if(number == 0) {
    new Guess().doGuess();
}
return;

So all in all the code looks like this:

System.out.println("Do you want to play again? (enter 0 for yes and 1 for no): ");
int number;
while(true) {
    try {
        number = input.nextInt();
        if(number < 0 || number > 1) {
            throw new InputMismatchException();
        }
        break;  
    }

    catch(InputMismatchException e) {
        System.out.println("Enter 0=yes or 1=no");
        input.nextLine();
    }
}
if(number == 0) {
    new Guess().doGuess();
}
return;

Don't forget to add the following import-statements:

import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
2 of 2
0

Try this. Basically, if the user responds with "yes" , we will call the function again.

    if (tries > 9) {
            System.out.println("You should be able to do better!"
                    + " You have hit your ten guess limit.  The number" + " was: " + answer);
            System.out.println("Do you want to play again? (yes/no): "); // modified line
            if("yes".equalsIgnoreCase(input.next())){ // newly added if block
                answer = generateRandomNumber();
                tries=0;
                i=0;
                win = false;
                doGuess();
            }
            return;
        }
🌐
Chegg
chegg.com › homework-help › questions-and-answers › hi-need-help-developing-program-java-thats-guessing-game-modify-program-plays-number-guess-q10478275
Solved: Hi, I Need Help Developing A Program In Java Thats... | Chegg.com
A new game should begin if the user's response starts with a lower- or upper-case Y. For example, answers such as "y", "Y", "yes", "YES", "Yes", or "yeehaw" all indicate that the user wants to play again.