简体   繁体   中英

Problem with while loop in guessing game?

I am facing issues to make game over!

I create int numberOfTries = 0 and trying to make game over if the user has numberOfTries >4. I couldn't figure out where the issue is?

        do {
            int theNumber = (int) (Math.random() * 100 +1 );
            int guess = 0;
            int numberOfTries = 0;

            while (guess != theNumber){
                System.out.println("Guess a number between 1 and 100");
                guess = scan.nextInt();


                if (guess< theNumber) {
                    System.out.println(guess + " is too low.");
                    numberOfTries += 1 ;
                }


                else if ( guess > theNumber) {
                    System.out.println(guess + " is too high.");
                    numberOfTries += 1;
                }

                else if(guess == theNumber) {
                    System.out.println(guess + " is correct!");

                    if (numberOfTries == 1 ){
                        System.out.println("You are a  genius!");

                    }
                    else if (numberOfTries == 0){
                        System.out.println("You are a pure genius!");
                    }
                    else if (numberOfTries <=4 ){
                        System.out.println("It only took you " + numberOfTries +
                                " Good Job!");
                    }

                }
                else if (numberOfTries > 4){
                    System.out.println("Game over!");
                }

            }
            System.out.println("Would you like to play again?(y/n)");

            playAgain = scan.next();

        }while (playAgain.equalsIgnoreCase("y"));

        System.out.println("Thank you for playing this game! Good Bye!");

        scan.close();

    }
}

Can anyone explain this to me, please? Thank you

You should break the loop when your number of attempts exceeds 4. Not just print out the message.

Your if statment always entry in one of the three conditions, for this reason never entry in the last condition. One solution is change this:

else if (numberOfTries > 4){
                System.out.println("Game over!");
 }

For this:

if (numberOfTries > 4){
        System.out.println("Game over!");
        break;
 }

Your if else construct looks like this:

if (guess< theNumber) {

}
else if ( guess > theNumber) {

}
else if(guess == theNumber) {

}
else if (numberOfTries > 4){

}

So first you ask if the guess is smaller then the number, then if that is not the case if it is bigger, and then if it is not the case if it is equal.

Now at that point your if-else construct already has exhausted all possible states as a number has to be either bigger, smaller or equal to another number, so your last else if will never be entered.

Just change the last else if to a normal if:

if (numberOfTries > 4){

}

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