简体   繁体   中英

Number guessing game play again

Im having problems making my game play again. I have a boolean variable but when i enter yes or no the program just ends. Im having trouble making it restart the //Game loop. My boolean doesn't execute anything at the moment

    //Game

    System.out.println("Pick a number between 1-100");

    Scanner keyboard = new Scanner(System.in);
    Random rand = new Random();
    int number = rand.nextInt(100)+1;
    int round = 0;
    int count = 0;
    int guess = 0;
    int win = 0;

    while(win == 0)
    {
        round++;
        System.out.println("Round " + round);

        System.out.print("What is your first guess? ");
        guess = keyboard.nextInt();
        count++;

        if (guess == number)
        {
            if (count == 1)
            {
                System.out.println("You win in " + count + " guess.");
                ++win;
                break;
            }
        }
        else if (guess > number)
        {
            System.out.println("That's too high. Try again: ");
        }
        else if (guess < number)
        {
            System.out.println("That's too low. Try again: ");
        }

    }


    //Ask to play again
    boolean isValidAnswer;
    do
    {
        System.out.print("Would you like to play again (yes/no)? ");
        String 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(!isValidAnswer);
}

}

You are incrementing the round before the user wins the guess which is incorrect, so place it inside the if block which means that it will be incremented only when the guess is right as shown below, also initialize the round to 1 during the start of the game.

    int round = 1;
    int count = 0;
    int guess = 0;
    int win = 0;
    boolean showRound=true;
    while(win == 0) {
        if(showRound) {
            System.out.println("Round " + round);
        }
        System.out.print("What is your first guess? ");
        guess = Integer.parseInt(keyboard.nextLine());//Use scanner.nextLine()
        count++;

        if (guess == number) {
            showRound = true;
            round++;//Increment the round only after win
            System.out.println("You win in " + count + " guess.");
            ++win;
            break;
        } else if (guess > number) {
            System.out.println("That's too high. Try again: ");
            showRound=false;
        } else if (guess < number) {
            System.out.println("That's too low. Try again: ");
            showRound=false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM