2

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"));
        }
}       
2

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Technically you want (lowerBound + upperBound) >>> 1 if you want positive numbers and want to avoid overflows
-1

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.