0

我只是在做一个简单的猜谜游戏。如果用户输入 0,我需要我的 do-while 循环退出。按照它的方式,如果这是他们输入的第一个数字,它会正确退出,否则它只会随着猜测数的总和而增加。这是我的代码:

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);

我猜我的 while() 循环中的条件可能不正确。我从以前的方式稍微改变了它,它是 while(guess!=secretNumber); . 也许最好保持原样,我们应该以不同的方式对待输入 0?谢谢。

4

5 回答 5

3

问题是您增加了除第一个猜测之外的所有猜测:

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

所以他们需要输入-1猜测0并结束循环。他们还需要输入小于 1secretNumber才能正确猜测......

于 2013-06-30T19:36:03.050 回答
1

为什么在倒数第二行需要guess++?

去掉guess++或者注释掉就可以了

于 2013-06-30T19:39:34.703 回答
0

这是你应该做的: -

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
于 2013-06-30T19:42:47.477 回答
0

如果这是他们输入的第一个数字,它会正确退出,否则它只会随着猜测数的总和而增加

这一行导致了这一点,据我所知,您的代码是不需要的:

guess++;
于 2013-06-30T19:35:01.393 回答
0

把你的guess == 0条件放在第一位。由于 0 小于任何生成的数字,它将首先进入该条件。

于 2013-06-30T19:33:46.023 回答