问题
I have hit this point on an assignment and I was hoping for some guidance. Basically the program is supposed to have a user think of a number between 1-100 and then ask if it is higher or lower than 50. Then the program outputs the midpoint until of the range until the answer is correct. For example if 'h' was entered it would then ask if the number is 75, if the response is then 'l' it would ask if the number is 67, etc.
I think I have built the framework but I am really struggling with how to approach the next step in finding the midpoint. Any guidance would be greatly appreciated.
import java.util.Scanner;
public class numberguess
{
public static void main(String[] args)
{
String shouldPlayAgain = "y";
String response = "h";
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Guess a number between 1 and 100.");
System.out.print("Is it 50? (h/l/c): ");
response = keyboard.nextLine();
if (response.equals("h"))
{
System.out.println("Is it 75? (h/l/c): ");
}
if (response.equals("l"))
{
System.out.println("Is it 25? (h/l/c): ");
}
System.out.print("Great! Do you want to play again? (y/n): ");
shouldPlayAgain = keyboard.nextLine();
}
while (shouldPlayAgain.equals("y"));
}
}
回答1:
I'm not going to write the solution out, but I'll try to point you in the right direction. Hopefully that'll get you going and on the right track to implement the solution yourself. Feel free to ask specific questions if anything is unclear.
You need to create two variables to keep track of your lower and upper bounds for guessing.
int lowerBound = 0;
int upperBound = 100;
Then you iteratively guess the middle, which is:
(upperBound + lowerBound) / 2;
Let's say you guess 50
. Then the user enters H
. This means that the number is bigger than 50, but less than 100. So now you know that your new lower bound is 50, set:
lowerBound = 50;
And repeat the process, this time (upperBound + lowerBound) / 2;
gives you 75, and so on. You know you're done guessing if lowerBound equals upperBound. All that remains is to structure this process in a loop, and you're done.
回答2:
Basically in your question you want user to enter or think of a number between 1-100 and ask if it is higher or lower than 50. So for this you need to have two variables like for example
int first = 0; and int last = 100;
And to get the midpoint until the answer is correct
int middle = (first + last) / 2;
Here you can refer binary search algorithm for more information.
Hope it helps!!
回答3:
include
using namespace std;
//function prototypes
void playOneGame();
void getUserResponseToGuess(int guess, char& result);
int getMidPoint(int low, int high);
//main method
int main()
{
//declare variables
char response;
//promt the users choice
cout << "Ready to play (y/n)? ";
cin >> response;
//reapeat the loop until the user'c choice is no
while (response == 'y')
{
//Call the function initially
playOneGame();
cout<<"Great! Do you want to play again (y/n)? ";
cin >> response;
}
cout << "Thanks for playing!" << endl;
system("pause");
return 0;
}
//implement the function playOneGame
void playOneGame()
{
//initialGuess
int guess = 50; // initial guess
int low = 1; // initial low point
int high = 100; // initial high point
char result;
cout<< "Think of a number between 1 to 100. " << endl;
//call the function to guess the users number //through binary search
getUserResponseToGuess(guess, result);
//Repeat the loop, until the answer is correct
while (result != 'c')
{
//If the answer is high
if (result == 'h')
{
low = guess;
//compute the midpoint
guess = getMidPoint(low, high);
//call the function
getUserResponseToGuess(guess, result);
}
else
{
high = guess;
guess = getMidPoint(low, high);
getUserResponseToGuess(guess, result);
}
}
}
//This function inputs the computer guess and displays it //to the user.
void getUserResponseToGuess(int guess, char& result)
{
cout << "Is it " << guess << " (h/l/c)?"<< endl;
cin >> result;
}
//This function inputs the low and high and it returns //the midpoint between them.
int getMidPoint(int low, int high)
{
return (low + high) / 2;
}
来源:https://stackoverflow.com/questions/18711100/stuck-on-a-java-assignment-binary-search-algorithm