Assignemnt #66 Hi-Lo with Limited Tries
Code
/// Name: Jake Johnson
/// Period: 7
/// Program Name: LimitedTries
/// File Name: LimitedTries.java
/// Date Finished: 12/8/2015
import java.util.Scanner;
import java.util.Random;
public class LimitedTries
{
public static void main (String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int guess;
int tries = 2;
int number = 1 + r.nextInt(100);
System.out.println("I'm thinking of a number between 1 and 100. Try to guess it. You have 7 guesses");
System.out.print("First guess: ");
guess = keyboard.nextInt();
while (guess != number && tries < 7)
{
if(guess > number)
{
System.out.println("Sorry, you are too high.");
System.out.print("Guess #" + tries + ": ");
guess = keyboard.nextInt();
tries++;
}
else if(guess < number)
{
System.out.println("Sorry, you are too low.");
System.out.print("Guess #" + tries + ": ");
guess = keyboard.nextInt();
tries++;
}
}
if (guess == number)
{
System.out.println("You guessed it! What are the odds?!?");
}
else if (guess != number)
{
System.out.println("Sorry, you didn't guess it in 7 tries. You lose. It was " + number + " .");
}
}
}
Picture of the output