卡在Java赋值,二进制搜索算法上 [英] Stuck on a java assignment, binary search algorithm

查看:51
本文介绍了卡在Java赋值,二进制搜索算法上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在任务上达到了这一点,我希望能提供一些指导.基本上,该程序应该让用户想到一个介于1到100之间的数字,然后询问该数字是高于还是低于50.然后,程序将输出中点,直到范围正确为止,直到答案正确为止.例如,如果输入"h",它将询问数字是否为75,如果响应为"l",则询问数字是否为67,等等.

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

推荐答案

我不会写出解决方案,但我会尽力指出正确的方向.希望这能使您走上正确的道路,以自己实施解决方案.如果有任何不清楚的地方,随时提出具体问题.

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;

比方说,您猜到了50.然后,用户输入H.这意味着该数字大于50,但小于100.所以现在您知道新的下界为50,设置:

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;

并重复该过程,这次(upperBound + lowerBound) / 2;给您75,依此类推.您知道您已经完成了对lowerBound等于upperBound的猜测.剩下的就是以循环方式构造此过程,您就完成了.

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.

这篇关于卡在Java赋值,二进制搜索算法上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆