while(looper)
{
char higher = 'h';
char lower = 'l';
guess = getMidpoint(min,max);
char respons = getUserResponseToGuess(guess);
if(respons == 'c'){
looper = false;
}
else if(respons == higher)
{
min = min + 1;
getMidpoint(min,max);
}
else if(respons == lower)
{
max = min - 1;
getMidpoint(min,max);
}
}
public static int getMidpoint(int low, int high)
{
int midpoint;
midpoint = (high + low) / 2;
return midpoint;
}
So basically this is a guessing game that uses binary search, and the getMidpoint method gets the mid of the two numbers, min is 1 and max is 100. h stands for high and l stands for lower and c stands for correct. I have everything correct but the guess keeps coming out weird, like it give me random numbers. How can i update the max and min so it comes out correct? Hopefully i worded this right.
else if(respons == higher)
{
min = min + 1;
getMidpoint(min,max);
}
else if(respons == lower)
{
max = min - 1;
getMidpoint(min,max);
}
This part is strange. If the guess should be higher, you should do min = midpoint + 1
instead of only increasing min
by 1. And when it should be lower, you should do max = midpoint - 1
.
You haven't shown the implementation of getMidpoint()
, so I'm not sure whether guess
could be used as the new min
/max
or not.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments