Assignment #66 Hi-Lo with Limited Tries
Code
/// Name: Koosha Kimelman
/// Period: 7
/// Program Name: Hi-Lo with Limited Tries
/// File Name: HighLowTries.java
/// Date Completed: 11/10/15
import java.util.Scanner;
import java.util.Random;
public class HighLowTries {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int guess, answer, tries;
tries = 0;
answer = (1+r.nextInt(100));
System.out.println("I'm thinking of a number between 1 and 100. You have 7 guesses.");
System.out.print("Guess #" + (tries+1) + ": ");
guess = keyboard.nextInt();
tries++;
while (guess != answer && tries < 7) {
if (guess > answer)
System.out.println("Too high.");
else if (guess < answer)
System.out.println("Too low.");
System.out.print("Guess #" + (tries+1) + ": ");
guess = keyboard.nextInt();
tries++;
}
if (guess == answer)
System.out.println("You got it right. Nice job.");
else if (tries >= 7)
System.out.println("You ran out of tries. The answer was " + answer + ".");
}
}
Picture of the output