Assignemnt #66

Code

/// Name: Sam Menar
/// Period: 6
/// Program Name: HiLo
/// File Name: HiLo.java
/// Date Finished: 12/17/15

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


    

Picture of the output

Assignment 13