Programming Assignment #68
Code
/// Name: Dylan Sleeper
/// Period: 6
/// Program Name: guess
/// File Name: guess.java
/// Date Finished: 1/5/2016
import java.util.Scanner;
public class guess{
public static void main(String[] args){
System.out.println("Pick a number 1-1000 and I'll try to guess it");
int hi = 1000;
int low = 1;
int guess = (int)Math.round((hi + low) / 2);
String ans = "";
Scanner keyboard = new Scanner(System.in);
while(ans.compareTo("c") != 0){
System.out.println("My guess is " + guess + ". Is that to high(h), low(l), or is it right(c)?");
ans = keyboard.next();
if(ans.equals("h")){
hi = guess;
}
else if(ans.equals("l")){
low = guess;
}
guess = (int)Math.round((hi + low) / 2);
}
System.out.println("Wow, I actually got it!");
}
}
Outputs