Assignment #66 and Hi Lo with tries
Code
///Name: Tim Gibson
///Period: 6
///Project Name: HiLo with tries
///File Name: HiLo2.java
///Date Finished: 10/20/15
import java.util.Random;
import java.util.Scanner;
public class HiLo2
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int guess, num, tries = 0, mtries, gnum;
//gnum is for number of guesses. mtries is maxtries
gnum = 1;
num = 1 + r.nextInt(100);
mtries = 7;
System.out.println( "I'm thinkin of a number between 1-100. You have 7 guesses." );
System.out.print( "First guess: " );
guess = keyboard.nextInt();
gnum++;
tries++;
while ( guess != num && tries < mtries )
{
if ( guess < num )
System.out.println( "Sorry, you are too low." );
else if ( guess > num )
System.out.println( "Sorry, you are too high." );
System.out.print( "Guess # " + gnum + ": " );
guess = keyboard.nextInt();
gnum++;
tries++;
}
if ( guess == num )
System.out.println( "You Guessed it! What are the odds?" );
else if ( tries >= mtries )
System.out.println( "Sorry, you didn't guess it in 7 tries. You lose." );
}
}
Picture of the output