Assignemnt #67
Code
/// Name: Connor Siri
/// Period: 7
/// Program Name: Hi
/// File Name: Hi.java
/// Date Finished: 11/04/2015
import java.util.Random;
import java.util.Scanner;
public class Hi
{
public static void main( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int x = 1 + r.nextInt(100);
int tries = 0;
System.out.println("Pick a number between 1 and 100");
System.out.print("Guess #" + (tries+1) + ": ");
int entry = keyboard.nextInt();
tries++;
while ( entry != x && tries < 7 )
{
if (entry > x)
{
System.out.println("\n Sorry to high");
System.out.print("guess #" + (tries+1) + ": ");
entry = keyboard.nextInt();
tries++;
}
else if ( entry < x )
{
System.out.println("\n Sorry to Low");
System.out.print("guess #" + (tries + 1) + ": ");
entry = keyboard.nextInt();
tries++;
}
}
if ( entry == x )
{
System.out.println("\nYou got it right");
}
else if ( tries >= 7 )
{
System.out.println("You didn't guess it in 7 tries");
}
}
}
Picture of the output