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