Assignemnt #66: Hi-Lo with Limited Tries
Code
///Name: Cassie
///Period: 6
///Program Name: Hi-Lo with Limited Tries
///File Name: hilolimit.java
///Date Finished: 12/8/15
import java.util.Random;
import java.util.Scanner;
class hilolimit
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess, ans = 1 + r.nextInt(100), tries = 0, maxTries = 7;
System.out.print( "Pick a number between 1-100. You have 7 guesses." );
System.out.print( " First Guess: " );
guess = keyboard.nextInt();
tries++;
while ( guess != ans && tries < maxTries )
{
System.out.println( "Incorrect. try again" );
System.out.println( "Guess # " + ( tries + 1 ));
guess = keyboard.nextInt();
tries++;
}
if ( guess == ans )
System.out.println( "You guessed it!" );
else if ( tries >= maxTries )
System.out.println( "Sorry, you didn't guess it in 7 tries. You lose." );
}
}
Picture of the output