Assignemnt #66 and LimitedHiLo
Code
/// name: Leila Ordukhani
/// period: 6
/// program name: Fifty-Sixth Program
/// file name: LimitedHiLo.java
/// date finished: 12/7/15
import java.util.Random;
import java.util.Scanner;
public class LimitedHiLo
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int response, x = 1 + r.nextInt(100);
int tries = 0;
int guess;
System.out.println("I'm thinking of a number between 1-100. You have 7 guesses.");
System.out.print("First guess: ");
guess = keyboard.nextInt();
tries++;
if (guess < x)
System.out.println("Sorry, you are too low.");
else if (guess > x)
System.out.println("Sorry, you are too high.");
while ( guess != x && tries < 7)
{
System.out.print("Guess # " + (tries+1) + ": ");
guess = keyboard.nextInt();
tries++;
if (guess < x)
System.out.println("Sorry, you are too low.");
else if (guess > x)
System.out.println("Sorry, you are too high.");
}
if (tries >= 7)
System.out.println("Sorry, you didn't guess it in 7 tries. You lose.");
else if (guess == x)
System.out.println("You guessed it! What are the odds?");
}
}
Picture of the output