Assignment #66: Hi-Lo with Limited Tries
Code
/// Name: Kiran O'Farrell
/// Program Name: Hi-Lo with Limited Tries
/// File Name: HiLoLimited.java
/// Date Completed: 1/20/16
import java.util.Random;
import java.util.Scanner;
public class HiLoLimited {
public static void main (String[] args) {
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int secret = 1 + r.nextInt(100);
int guess;
int tries = 0;
System.out.println("I'm thinking of a number from 1-100. Try to guess the secret number!");
guess = keyboard.nextInt();
while (guess != secret && tries < 7) {
if (guess > secret) {
System.out.println("That's too high. Guess lower.");
tries++;
guess = keyboard.nextInt();
}
if (guess < secret && tries < 7) {
System.out.println("That's too low. Guess higher.");
tries++;
guess = keyboard.nextInt();
}
}
if (guess == secret) {
System.out.println("Great job! You guessed correctly!");
}
if (tries >= 7) {
System.out.println("Sorry, you ran out of tries.");
}
}
}
Picture of the output