public class Pattern {
    public static void main(String[] args) {
        int lowest = 0, highest = 100, initialGuess, UserInput;
        boolean CorrectGuess = false;
        int countGuess = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("Think a number between 0 and 100.");

        int[] arr = new int[101];
        for(int i = 0; i <= 100; i++)
            arr[i] = i;

        while(!CorrectGuess) {
            int midRange = lowest + (highest - lowest) / 2;
            initialGuess = arr[midRange];
            countGuess++;

            System.out.println("Is " + initialGuess + " the number?");
            System.out.println("Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:");

            UserInput = input.nextInt();

            if(UserInput == 1){
                CorrectGuess = true;
                System.out.println("Guessed correctly in " + countGuess + " tries!");
            }
            else if(UserInput == 2)
                lowest = midRange;
            else if(UserInput == 3)
                highest = midRange;
        }

        boolean validAnswer = false;

        while (!validAnswer){

            System.out.println("Is " + initialGuess + " the number?");
            System.out.println("Enter 1 if this was a correct guess, 2 if your number is higher, and 3 if your number is lower:");

            UserInput = input.nextInt();

            if(UserInput == 1){
                validAnswer = true;
                System.out.println("Guessed correctly in " + countGuess + " tries!");
            }
            else if(UserInput != 2 || UserInput != 3)
                System.out.println("ERROR! Please restart the program and start again!");
        }
    }
}