Assignment #60

Code

  /// Name: Sam Wilson
  /// Period: 6
  /// Program Name: lohi
  /// File Name: lohi.java
  /// Date Finished: 12/24/2015

  import java.util.Random;
    import java.util.Scanner;
    
    public class lohi
    {
        public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            
            int hi, lo, guess;
            hi = 1000;
            lo = 1;
            guess = (lo + hi)/2;
            
            String answer;
            
            System.out.println("Think of a number from 1 to 1000, I'll try to guess it.");
            System.out.println("My guess is "+guess+" Am I too (h)igh, too (l)ow, or (c)orrect?");
            System.out.print("> ");
            answer = keyboard.next();
            
            while (!answer.equals("c"))
            {
                if (answer.equals("h"))
                {
                        hi = guess;
                        guess = (lo + hi)/2;
                        System.out.println("My guess is "+guess+" Am I too (h)igh, too (l)ow, or (c)orrect?");
                        System.out.print("> ");
                        answer = keyboard.next();
                }
                else if (answer.equals("l"))
                {
                        lo = guess;
                        guess = (lo + hi)/2;
                        System.out.println("My guess is "+guess+" Am I too (h)igh, too (l)ow, or (c)orrect?");
                        System.out.print("> ");
                        answer = keyboard.next();
                }
                else
                {
                    System.out.println("ERROR");
                }
                
            }
                
                System.out.println("I Win you Lose!!");
        }
    }
            
                
            
                        
                
    

Picture of the output

Assignment 31