Assignemnt 66 and Hi-Lo With Limited Tries
Code
/// Name: Xinting Chen
/// Period: 7
/// Program Name: Hi-Lo With Limited Tries
/// File Name: HiLoLimited.java
/// Date Finished: 12/8/2015
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class HiLoLimited
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int secretnumber = 1 + r.nextInt(100), guess, attempts;
attempts = 0;
System.out.println("I'm thinking of a number between 1-100. You have 7 guesses. Good luck!");
System.out.print("Guess #" + (attempts+1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
attempts++;
while (guess != secretnumber && attempts < 7)
{
if (guess > secretnumber)
{
System.out.println("Sorry, you are a little high.");
System.out.print("Guess #" + (attempts+ 1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
attempts++;
}
else if (guess < secretnumber)
{
System.out.println("Sorry, you are a little low.");
System.out.print("Guess #" + (attempts+ 1) + ": ");
guess = keyboard.nextInt();
System.out.println("");
attempts++;
}
}
if (guess == secretnumber)
{
System.out.println("You guessed it!");
}
else if (attempts >= 7)
{
System.out.println("Sorry, you didn't get it in 7 tries. You lose :(");
}
}
}
Picture of the output