import java.util.*; /* How many guesses does it take to find a number? */ public class HighLowGame { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.println("Pick a random number from 1-100 and I will guess it within 7 tries. After each guess, you will tell me if my guess was right, too low or too high. Please be honest!"); int count = 0, number = 50, low = 1, high = 100; char answer = ' '; while(answer!='C') { System.out.print("My guess is " + number + ". Is this correct (C), too high (H) or too low (L)? "); answer=in.next().toUpperCase().charAt(0); if(answer=='H') high = number-1; else if(answer=='L') low = number+1; number=(high + low) / 2; count++; if(count>7) answer='C'; } if(count <= 7) System.out.println("It took me " + count + " guesses."); else System.out.println("You must be cheating! I have guessed too many times"); } }