Code
/// Name: Afaf Nabeeha
/// Period: 7
/// Program Name: Reverse Hi-Lo
/// File Name: Reverse.java
/// Date Finished: 12/17/2015 public class Reverse
import java.util.Scanner;
public class Reverse
{
public static void main( String[] args )
{
int hi, lo, guess, newG;
String answer;
lo = 1;
hi = 1000;
guess = ( lo + hi ) / 2 ;
Scanner keyboard = new Scanner(System.in);
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 = ( guess + lo ) / 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();
if ( answer.equals("l") )
{
lo = guess;
guess = ( guess + 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 = ( guess + 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();
if ( answer.equals("h") )
{
hi = guess;
guess = ( guess + lo ) / 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();
}
}
}
System.out.println( "Ha! I am the greatest guesser in the world. " );
}
}
Picture of the output