Limit do-while to running 7 times unless given the correct input

Pejmon Hodaee picture Pejmon Hodaee · Oct 13, 2012 · Viewed 8k times · Source

I was having trouble getting the do-while loop to stop after the user has attempted to guess the number 7 times. I'm not sure exactly how to tell the loop to stop and display the timeout message.

Thank you, I appreciate any help!

Code:

import java.util.Random;
import java.util.Scanner;
public class numberGuesses {
     public static void main (String [] args){
            Random rand = new Random();
            int inputNumber;
            int randomNumber = rand.nextInt(100) + 1;
            int guessingLimit = 7;

            System.out.println("Rand Number = " +randomNumber); //Temp to test program with correct number 


            Scanner scan = new Scanner (System.in);

            do {
                System.out.print("Please enter a number: ");
                inputNumber = scan.nextInt();

                if (inputNumber > randomNumber)
                            System.out.println("That's too high!");
                if (inputNumber < randomNumber)
                            System.out.println("That's too low!");
       //       else { 
       //       guessingLimit--;
       //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
       //       break;
       //       }
            } while (inputNumber != randomNumber);
                System.out.println("That's right!"); //This line when they guess correctly  
    }

}

Correct Output 1:

----exec: java numberGuesses
Rand Number = 67
Please enter a number: 89
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 67
That's right!

Correct Output 2:

----exec: java numberGuesses
Rand Number = 38
Please enter a number: 55
That's too high!
Please enter a number: 66
That's too high!
Please enter a number: 1
That's too low!
Please enter a number: 22
That's too low!
Please enter a number: 44
That's too high!
Please enter a number: 8
That's too low!
Please enter a number: 99
That's too high!
Too bad! The number was 38!

Answer

Yogendra Singh picture Yogendra Singh · Oct 13, 2012

Before do, write,

  int numAttempts = 0;

within do

  numAttempts++;

while condition:

  while (inputNumber != randomNumber && numAttempts < 7);

Complete:

       int numAttempts = 0;
       do {
            System.out.print("Please enter a number: ");
            inputNumber = scan.nextInt();

            if (inputNumber > randomNumber)
                        System.out.println("That's too high!");
            if (inputNumber < randomNumber)
                        System.out.println("That's too low!");
   //       else { 
   //       guessingLimit--;
   //       System.out.println("Too bad! The number was " +randomNumber+ "!"); //Trying to have it print this timeout after 7 incorrect attempts
   //       break;
   //       }
          numAttempts++;
        } while (inputNumber != randomNumber && numAttempts < 7);

        if(inputNumber == randomNumber){
            System.out.println("That's right!"); 
        }else{
            System.out.println("Incorrect 7 attempts"); 
        }