Assignemnt #66 and Keep Guessing
Code
///Name: Daniel Tiffany- Appleton
///Period: 7
///Program Name: Keep Guessing
///Fill Name: KeepGuessing
///Date Finished: 11/16/15
import java.util.Random;
import java.util.Scanner;
public class KeepGuessing
{
public static void main( String [] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int answer, guess, tries;
System.out.println( "I'm thinking of a number between 1-100. What is it? You have 7 tries");
System.out.print( "Guess #1: ");
guess = keyboard.nextInt();
tries = 1;
answer = 1 + r.nextInt(100);
while( guess != answer && tries < 7 )
{
if ( guess > answer )
{
System.out.println( "You were too high." );
System.out.print( "Guess #" + (tries + 1) + ": " );
guess = keyboard.nextInt();
}
else if ( guess < answer )
{
System.out.println( "You were too low." );
System.out.print( "Guess #" + (tries + 1) + ": " );
guess = keyboard.nextInt();
}
tries++;
}
if ( guess == answer )
{
System.out.println( "That is correct, how did you get it. Did you cheat!" );
}
if ( tries == 7 )
{
System.out.println( "Sorry, you ran out of tries." );
}
}
}
Picture of the output