Code
/// Name: Afaf Nabeeha
/// Period: 7
/// Program Name: Hi-Lo with Limited Tries
/// File Name: Tries.java
/// Date Finished: 12/15/2015 public class Tries
import java.util.Random;
import java.util.Scanner;
public class Tries
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int randomNumber = 1+ r.nextInt(100);
int guess, guessNumber, max;
guessNumber = 0;
max = 7;
System.out.println( "I'm thinking of a number between 1-100. You have 7 guesses. " );
System.out.print( "First guess: " );
guess = keyboard.nextInt();
guessNumber++;
while ( randomNumber != guess && guessNumber <= max )
{
if ( randomNumber > guess )
{
guessNumber++;
System.out.println( "Sorry, you are too low. " );
System.out.print( "Guess # " + guessNumber + ":" );
guess = keyboard.nextInt();
}
else if ( randomNumber < guess )
{
guessNumber++;
System.out.println( "Sorry your guess is too high. " );
System.out.print( "Guess # " + guessNumber + ":" );
guess = keyboard.nextInt();
}
}
if ( randomNumber == guess && guessNumber <= max )
{
System.out.println( "You guessed it! What are the odds?!?" );
}
else if (randomNumber != guess && guessNumber >= max )
{
System.out.println( "Sorry you didn't guess it in 7 tries. You lose. " );
}
}
}
Picture of the output