简体   繁体   中英

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. 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. I changed it slightly from how it was before, which was while(guess!=secretNumber); . Maybe that is better left as it was and we should treat entering a 0 differently? 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. They also need to enter one less than secretNumber to have the correct guess....

Why do you need guess++ on the second last line?

Remove the guess++ or comment it out and you should be fine

Put your guess == 0 condition first. Since 0 is less than any generated number it will enter that condition first.

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

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