Assignment #59

Code

  /// Name: Sam Wilson
  /// Period: 6
  /// Program Name: hilo
  /// File Name: hilo.java
  /// Date Finished: 12/13/2015

  import java.util.Random;
    import java.util.Scanner;
    
    public class hilo
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            
            int guess, g1, n = 0;
            
            
            Random r = new Random();
            
            g1 = 1 +r.nextInt(100);
            System.out.println( "I'm thinkin of a number between 1-100. Try to guess it." );
            System.out.println();
            System.out.print( "> ");
            guess = keyboard.nextInt();
            n++;
            
           
            
            while (n < 7 && guess != g1)
           {
            if ( guess < g1 )
            {
                System.out.println( "Sorry you are too low. You have " +(7-n)+ " more tries. Try Again");
            }
            else if ( g1 < guess )
            {
                System.out.println( "Sorry, you guessed too high. You have " +(7-n)+ " more tries. Try again");
            }
                System.out.println();
            System.out.print( "> ");
            guess = keyboard.nextInt();
            n++;
           }
               
               
               
            if ( guess == g1 )
            {
                System.out.println( "You guessed it!" );
            
            }
            else if ( n == 7)
            {
              System.out.println("You ran out of tries"); 
            }
            
        
        }
    }
    

Picture of the output

Assignment 31