import java.util.Scanner;
import java.util.Random;
public class GuessingGame{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String input;
boolean playing = true;
System.out.println("Pick a number from 1 to 100, and I will try to guess it!");
while(playing){
Random rand = new Random();
int high = 100;
int low = 0;
int guessesTaken = 0;
int guess;
boolean correct = false;
boolean answered = false;
while(!correct){
if(high - low - 1 > 0){
guess = rand.nextInt(high - low - 1) + low + 1;
}
else{
answered = true;
correct = true;
guess = 0;
System.out.println("You're a liar!");
}
guessesTaken++;
while(!answered){
System.out.printf("\nIs %s your number? [Yes (y) / No, mine is higher (h) / No, mine is lower (l)]\n", guess);
input = scan.next();
if(input.equalsIgnoreCase("y")){
correct = true;
System.out.printf("Yay, I guessed correctly! And it only took %s guesses!\n", guessesTaken);
answered = true;
}
else if(input.equalsIgnoreCase("h")){
System.out.println("Dang.");
low = guess;
answered = true;
}
else if(input.equalsIgnoreCase("l")){
System.out.println("Dang.");
high = guess;
answered = true;
}
}
answered = false;
}
answered = false;
while(!answered){
System.out.println("Do you want to play again? [Yes (y) / No (n) ]");
input = scan.next();
if(input.equalsIgnoreCase("n")){
playing = false;
answered = true;
}
else if(input.equalsIgnoreCase("y")){
playing = true;
answered = true;
System.out.println("Great! Pick another number from 1 to 100.");
}
}
}
}
}