Assignment #68

Code

    ///Name: Cody Swain
    ///Period: 6
    ///Project Name: ReverseHiLo
    ///File Name: ReverseHiLo.java
    ///Date: 12/4/15
    
    import java.util.Scanner;
    
    public class ReverseHiLo
    {
        public static void main( String[] args )
        {
            Scanner keyboard = new Scanner(System.in);
            int hi, lo, guess;
            lo = 1;
            hi = 1000;
            String answer;
            
            System.out.println( "Think of a number from 1 to 1000. I'll try to guess it. ");
            System.out.println( "My first guess is " + (lo + hi)/2 + ". Am I too (h)igh, too (l)ow, or (c)orrect? ");
            answer = keyboard.next();
            
            while ( !answer.equals("c")){
            
                if ( answer.equals("h")){
                    hi = (lo+hi)/2;
                    System.out.println( "My guess is " +(lo + hi)/2 + ". Am I too (h)igh, too (l)ow, or (c)orrect? ");
                }
                else if ( answer.equals("l")){
                    lo = (lo+hi)/2;
                    System.out.println( "My guess is " +(lo + hi)/2 + ". Am I too (h)igh, too (l)ow, or (c)orrect? ");
                }
                answer = keyboard.next();
            }
            
            if ( answer.equals("c") )
                System.out.println( "Ha! I am the greatest guesser in the world! ");
                
        }
    }
                
                
        
    

Picture of the output

Assignment 68