Assignemnt #66: Hi and Lo Guesses with While Loops
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: Hi and Lo guesses with While Loops
/// File Name: HiLoWhileLoop.java
/// Date Finished: 12/10/2015
import java.util.Scanner;
import java.util.Random;
public class HiLoWhileLoop
{
public static void main( String[] args )
{
int guess, guessCounter, number, maxTries;
maxTries = 7;
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
number = 1 + r.nextInt(100);
guessCounter = 0;
System.out.println( "I'm thinking of a number between 1-100. You have 7 guesses to get it correct " );
System.out.print( "First guess : " );
guess = keyboard.nextInt();
guessCounter++;
while ( guess != number && guessCounter < maxTries )
{
if ( number < guess )
{
guessCounter++;
System.out.println( " The number you choose was too high " );
System.out.println( " Your guess # is " + guessCounter );
guess = keyboard.nextInt();
}
else if ( number > guess )
{
guessCounter++;
System.out.println( " The number you choose was too low " );
System.out.println( " Your guess # is " + guessCounter );
guess = keyboard.nextInt();
}
}
if ( guess == number && guessCounter < maxTries )
{
System.out.println ( "Nice job you correctly guessed the random number! " );
}
else if ( guess != number && guessCounter >= maxTries )
{
System.out.println( " You have run out of tries to correctly guess the number sorry. " );
}
}
}
Picture of the output