0

When number 0 is typed after the guess is correct, it should replay the game. It just returns the message "0 to play again and 1 to quit"

public static void main(String[] args) {
    boolean game = true; //true means the game will continue playing, false stops the game
    Scanner input = new Scanner(System.in);
    System.out.println("Guess: ");
    while (game) {
        HiLo mainGame = new HiLo();
        mainGame.generateRandomNumber();
        boolean guessCorrect = false;
        while (!guessCorrect) {
            int inputted = input.nextInt();
            if (inputted == 101) {
                guessCorrect = true;
                System.out.println("quitted");
            }
            System.out.println(mainGame.checkGuess(inputted));
            mainGame.getGuessCounter();
            if (mainGame.checkGuess(inputted) == "Correct! You got it right!") {
                guessCorrect = true;
                System.out.println("Correct after " + mainGame.getGuessCounter() + " guesses");
            }
        }
        while (guessCorrect = true) {
            System.out.println("Would you like to play again? (0 : YES) (1 : NO)");
            System.out.println("Your answer: ");
            int answer = input.nextInt();
            //issues with inputting 0 and restarting game
            if (answer == 0) {
                game = true;
            }
            else if (answer == 1) {
                guessCorrect = true;
                System.out.println("Game Over");
            }
            else {
                System.out.println("Error, can't understand the meaning"); 
                guessCorrect = false;
            }
        }
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
unknown
  • 11
  • 1
    `while (guessCorrect = true)` That should be just `while (guessCorrect)` – Abra Dec 23 '21 at 04:58
  • 1
    `if (mainGame.checkGuess(inputted) == "Correct! You got it right!")` I couldn't find the code for method `checkGuess` but I assume it returns a string. Please refer to [How do I compare strings in Java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Abra Dec 23 '21 at 05:09
  • Hi OP. Let us know if any the answer helped. If it answered your question, then accept it. That way others know that you've been (sufficiently) helped. Also see [What should I do when someone answers my question](https://stackoverflow.com/help/someone-answers)? – Donnald Cucharo Jan 04 '22 at 05:31

1 Answers1

0

Change while (guessCorrect = true) to while (guessCorrect). Set guessCorrect to false to exit the while (guessCorrect) loop and proceed to restarting or exiting the game. Make sure to set the game to false if answer is not equal to 0 to exit the game.

        while (guessCorrect) {
            System.out.println("Would you like to play again? (0 : YES) (1 : NO)");
            System.out.println("Your answer: ");
            int answer = input.nextInt();
            //issues with inputting 0 and restarting game
            if (answer == 0) {
                game = true;
            }
            else if (answer == 1) {
                System.out.println("Game Over");
                game = false;
            }
            else {
                System.out.println("Error, can't understand the meaning"); 
                game = false;
            }
            
            guessCorrect = false;
        }
Catherine O
  • 943
  • 1
  • 9