简体   繁体   English

当用户输入0时无法弄清楚如何退出do-while循环

[英]Can't figure out how to handle quitting do-while loop when user enters 0

I'm just making a simple guessing game. 我只是在做一个简单的猜谜游戏。 I need my do-while loop to exit if the user enters 0. The way it stands, it quits properly if that's the first number they enter, but otherwise it just increments as as sum of number of guesses. 如果用户输入0,我需要执行do-while循环才能退出。按它的方式,如果这是他们输入的第一个数字,则它会正确退出,否则,它会随着猜测数的总和而递增。 Here's my code: 这是我的代码:

System.out.println("The system will generate a random number between 1 and 100."
            + " Your goal is to guess this number.");
    do
    {

    Random secretNumberGen = new Random();
    int secretNumber = secretNumberGen.nextInt(100)+1;

    System.out.println("Enter a number between 1 and 100: ");
    guess = scan.nextInt();
    do
       {

        if(guess==secretNumber)
        {
            System.out.println("You guessed the number!");
        }
        else if(guess<secretNumber)
          {
         System.out.println("Your guess is too low! Guess again");
         numGuess++;
          }
        else if(guess>secretNumber)
          {
         System.out.println("Your guess is too high! Guess again");
         numGuess++;
          }
        else if(guess==0)
               break;
        System.out.println("Your number: \n");
        guess = scan.nextInt();
        guess++;
       }
    while(guess!=0&&guess!=secretNumber);

I'm guessing the condition in my while() loop may be incorrect. 我猜测while()循环中的条件可能不正确。 I changed it slightly from how it was before, which was while(guess!=secretNumber); 我从以前的那段时间(guess!= secretNumber)稍作更改。 . Maybe that is better left as it was and we should treat entering a 0 differently? 也许现在更好,我们应该以其他方式输入0? Thanks. 谢谢。

The problem is that you increment all guesses other than the first one: 问题是您增加了除第一个猜测以外的所有猜测:

    System.out.println("Your number: \n");
    guess = scan.nextInt();
    guess++;

So they need to enter -1 to have a guess of 0 and end the loop. 因此,他们需要输入-1才能猜测为0并结束循环。 They also need to enter one less than secretNumber to have the correct guess.... 他们还需要输入比secretNumber小一的secretNumber以进行正确的猜测。

Why do you need guess++ on the second last line? 为什么您需要在最后第二行使用guess ++?

Remove the guess++ or comment it out and you should be fine 删除猜测++或将其注释掉,您应该会没事的

Put your guess == 0 condition first. 首先将您的guess == 0条件。 Since 0 is less than any generated number it will enter that condition first. 由于0小于任何生成的数字,它将首先进入该条件。

it quits properly if that's the first number they enter, but otherwise it just increments as as sum of number of guesses 如果这是他们输入的第一个数字,它会正确退出,否则它将作为猜测总数的总和而增加

This line causes that, which as far as I understand your code is not needed: 此行导致这一点,据我了解,不需要您的代码:

guess++;

This is what you should be doing:- 这是您应该做的:

System.out.println("The system will generate a random number between 1 and 100."
        + " Your goal is to guess this number.");
do
{

Random secretNumberGen = new Random();
int secretNumber = secretNumberGen.nextInt(100)+1;

System.out.println("Enter a number between 1 and 100: ");
guess = scan.nextInt();
do
   {
    // Exit condition is being checked first so that control does not enter
    // the less-than-secret-number-condition which would always be true for zero
    if(guess==0)
         break;

    // If its not zero, num of guesses is to be incremented
    numGuess++;

    if(guess==secretNumber)
    {
        System.out.println("You guessed the number!");
        break; // Exits if it is zero or the secret number
    }
    else if(guess<secretNumber)
      {
     System.out.println("Your guess is too low! Guess again");
     // Commenting out the increment as its already done
     // numGuess++;
      }
    else if(guess>secretNumber)
      {
     System.out.println("Your guess is too high! Guess again");
     // Commenting out the increment as its already done
     // numGuess++;
      }

    System.out.println("Your number: \n");
    guess = scan.nextInt();
    // guess++ has been removed as I think it serves no purpose. numGuess is the variable maintaining the count

   }
while(true); // Changed the condition here as the exit conditions have already bee dealt with within the loop

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

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