reubs
reubs

Reputation: 1

performing binary search in range 1 to 100 with user input

I am running a binary search program that takes user input chars 'l' for low 'h'for high and 'c' if value is correct. my first method returns the character my midpoint method is only reading the first character input. if I start at 50 and press 'h'the midpoint correctly raises to 75 but I can't get the program to read 'l' and lower the value to 50 again. the program only reads the first char input by the user but I need to update the value in answer. Is my while loop wrong or my decision statements? Thank you.

import java.util.Scanner;

public class PlayGuessingGame {
    public static char getUserresponseGuess(){  
        Scanner scan = new Scanner(System.in);
        char guesses = scan.next().charAt(0); 
        System.out.println("this is the user input " + guesses);
        return(guesses);
    }   

    public static int getMidpoint(int low, int high){ 
        int middle;
        //the midpoint is high + low divided by 2
        middle = (low + high)/ 2;

        System.out.println("is it " + middle);
        char answer =  getUserresponseGuess();

        System.out.println("this is the answer " + answer);

        //char get_input = getUserresponseGuess();
        //System.out.println("this is the output "+ get_input);
        while(low <= high){
            if(answer == 'h'){
                low = middle + 1; 
                System.out.println("this is low " + low);
                middle = (low + high)/2; 

                char new_answer = getUserresponseGuess();
                middle = (low + high)/2;
                System.out.println("is it" + middle);
                getUserresponseGuess();
            }
            //the number presented to the user is is too high, the midpoint is high -1
            else if(answer == 'l'){
                System.out.println("we're in the low portion");
                high = middle - 1;
                middle = (low + high) /2;
                System.out.print("is it " + middle);
                middle = (low + high) /2;
            }
            if(answer == 'c'){
                System.out.println("congrats!");
            }
            return(middle);
        }
        return(-1); 
    }

    public static void main(String[] args){         
        getMidpoint(1,100);
    }
}

Upvotes: 0

Views: 495

Answers (1)

AxelH
AxelH

Reputation: 14572

You have some mistake here. I will try to answer to every problem but I might miss some, please highlight some if I miss one. Let see this loop with only comment to be more readable

while(low <= high){
    if(answer == 'h'){
        //calculate higher
        //ask input #####
        //calculate higher
        //ask input #####
    } else if(answer == 'l'){
        //calculate lower
    }
    if(answer == 'c'){
        //Congrats
    }
    //end method
}

Evaluate

First, you are doing too many operation in the if(answer == 'h'). You are calculating twice middle,with a new input ask between. This should not be done.

The idea is to ask the user an input once per loop.

Loop or not ?

Since you have a return statement at the the loop, without any condition, it will be executed no matter what. So your loop only execute once (this is not really a loop then). The following statement will simply stop the loop and exit the method :

return(middle);

I believe this should be in the previous condition to only stop the method when the user input that this is correct:

if(answer == 'c'){
     System.out.println("congrats!");
     return(middle);
}

Switch

You could use a switch instead of these 3 condition, this would be cleaner,

if (answer == 'h')
else if (answer == 'l')
else if (answer == 'c')

So the above condition could be writen like

switch(answer){
    case 'h':

        break;
    case 'l':

        break;
    case 'c':

        break;
    default:

}

Get input

You need to get the input on each loop, so the easier would be to do it at the beginning of the loop, just after the while line. Even better, directly in the switch since you don't really need the input later like:

switch(getUserresponseGuess()){
    ...
}

Scanner

Well, you should not create a new instance of a Scanner every time, what you are doing in the method to read a value :

public static char getUserresponseGuess(){  
    Scanner scan = new Scanner(System.in);
    char guesses = scan.next().charAt(0); 
    System.out.println("this is the user input " + guesses);
    return(guesses);
}   

Set scan a global instance and instanciate it once, this would reduce the risk of data leak.

private static Scanner scan = new Scanner(System.in);
public static char getUserresponseGuess(){  
    ....
}

Upvotes: 2

Related Questions