import java.util.Random; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { boolean found = false; float highrange = 50; float lowrange = 50; float guess = 50; int tries = 0; Scanner reader = new Scanner(System.in); String answer; while (true) { System.out.print("Think of a number between 1 and 100, I will try and guess it. "); System.out.println("I will say a number \n If your number is greater type \"l\", if your number is smaller say \"h\".\n If I guess the correct number, type \"y\""); while (found == false) { System.out.println("Is the number " + (int) guess + " ?. "); highrange = (float) Math.ceil(highrange / 2); lowrange = (float) Math.ceil(lowrange / 2); //System.out.println("h " + highrange + " l " + lowrange); answer = reader.nextLine(); if (answer.equals("h")){ guess += highrange; tries++; } if (answer.equals("l")) { guess -= lowrange; tries++; } if (answer.equals("y")) { System.out.println("Awesome! It took me " + tries + " tries to get it right.\n Let's try again!"); guess = 50; highrange = 50; lowrange = 50; } if (guess > 100 || guess <= 0) { System.out.println("You've been lying to me -- remember, numbers from ONE to ONE HUNDRED."); guess = 50; highrange = 50; lowrange = 50; break; } } } } }