Assignment #66

Code

    /// Name: Mahima Choudhary
    /// Period: 6
    /// Program Name: Hi-Lo with Limited Tries
    /// File Name: HiLoLimited.java
    /// Date Finished: 12/12/2015
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class HiLoLimited
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            Random r = new Random();
            
            int secretNumber = 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 < secretNumber)
                System.out.println("Sorry, you are too low.");
            else if (guess > secretNumber)
                System.out.println("Sorry, you are too high.");
            
            while ( guess != secretNumber && tries < 7)
            {
                System.out.print("Guess # " + (tries+1) + ": ");
                guess = keyboard.nextInt();
                tries++;
                
                if (guess < secretNumber)
                    System.out.println("Sorry, you are too low.");
                else if (guess > secretNumber)
                    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 == secretNumber)
                System.out.println("You guessed it! What are the odds?");
        }
    }
    

Picture of the output

Assignment 66