Assignment 66

Code

///jack newsom
///period 5
///program HiLoLimited
///filename HiLoLimited.java
///date completed 10/8/15

import java.util.Random;
import java.util.Scanner;

public class HiLoLimited
{
    public static void main( String[] args )
    {
        Scanner bot = new Scanner(System.in);
        Random r = new Random();
        
        System.out.println("I'm thinking of a number between 1-100. You have 7 guesses.");
        System.out.print("First guess: ");
        int guess = bot.nextInt();
        
        int tries = 0; 
        tries++;
        
        int actual = r.nextInt(99) + 1;
        
        while ( guess != actual && tries <7 )
        {
            if ( guess > actual )
            {
                System.out.println("Too high.");
                tries++;
                System.out.println("Guess #" + tries + ": " );
                guess = bot.nextInt();
            }
            else
            {
                System.out.println("Too low.");
                tries++;
                System.out.println("Guess #" + tries + ": " );
                guess = bot.nextInt();
            }
        }
        
        if ( tries <= 7 && guess == actual )
            System.out.println("You guessed it! What are the odds?");
        else
            System.out.println("Sorry, you didn't guess it in 7 tries. You lose. (The number was " + actual + " )");
    }
}      
    

Picture of the output

Assignment X