Assignemnt #62 Hi Lo with Limited Trys
Code
///Name: JJ Deng
///Period: 6
///Program Name: Hi-Lo with limited tries
///File Name: Hi-LoLimit
///Date Finished: 1/7/16
import java.util.Random;
import java.util.Scanner;
public class HiLoLimit
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int number, n ,guess ;
number = 1 + r.nextInt(1000);
n = 0;
System.out.println("I'm thinking of a number between 1-1000. You have 20 guesses. Good luck!");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
while (guess != number && n < 20)
{
if (guess > number)
{
System.out.println("You were a little high.");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
}
else if (guess < number)
{
System.out.println("You were a little low.");
System.out.print("Guess #" + (n+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
n++;
}
}
if (guess == number)
{
System.out.println("You got it!");
}
else if (n >= 20)
{
System.out.println("Sorry you ran out of tries. you lose.");
}
}
}
Picture of the output