Assignment #68
Code
/// Name: Ian Stanko
/// Period: 5
/// Program Name: RHiLo
/// File Name: RHiLo.java
/// Date Finished: 1/8/2016
import java.util.Random;
import java.util.Scanner;
public class RHiLo
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int hi, lo, guess;
String response;
lo = 1;
hi = 1000;
guess = (lo + hi)/2;
System.out.println("Think of a number between 1-1000, and i'll try to guess it");
System.out.println("My guess is " + guess + " am I to (h)igh, (l)ow, or (c)orrect?");
System.out.print("> ");
response = keyboard.next();
while (!response.equals("c"))
{
if (response.equals("h"))
{
hi = guess;
guess = (lo + hi)/2;
System.out.println("My guess is " + guess + " am I to (h)igh, (l)ow, or (c)orrect?");
System.out.print("> ");
response = keyboard.next();
}
else if (response.equals("l"))
{
lo = guess;
guess = (lo + hi)/2;
System.out.println("My guess is " + guess + " am I to (h)igh, (l)ow, or (c)orrect?");
System.out.print("> ");
response = keyboard.next();
}
}
System.out.println("Yay! I got it!");
}
}
Picture of the output