简体   繁体   English

猜谜游戏中的while循环有问题吗?

[英]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.如果用户的 numberOfTries >4,我创建 int numberOfTries = 0 并尝试结束游戏。 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.当您的尝试次数超过 4 时,您应该打破循环。不仅仅是打印出消息。

Your if statment always entry in one of the three conditions, for this reason never entry in the last condition.您的 if 语句总是进入三个条件之一,因此永远不会进入最后一个条件。 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 else 构造如下所示:

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.现在,您的 if-else 构造已经用尽了所有可能的状态,因为一个数字必须大于、小于或等于另一个数字,因此您的最后一个 else if 永远不会被输入。

Just change the last else if to a normal if:只需将最后一个 else if 更改为正常 if:

if (numberOfTries > 4){

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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