0

我在这方面遇到了很多麻烦,很快就要到期了,我想知道是否有人知道如何解决我的问题。我必须创建一个程序,其中:“您的任务是实现一个基于二进制搜索原理工作的数字猜测器。在每一步中,计算机将查询间隔减半。当间隔包含单个数字时,它会宣布答案。程序的用户选择 1 到 100 之间的一个数字。然后要求计算机猜测这个数字。

示例输出如下:

Is your number greater than 50? (computer is asking this)
no (user responds with yes or no)
Is your number greater than 25?
no
Is your number greater than 13?
no
Is your number greater than 7?
yes
Is your number greater than 10?
yes
Is your number greater than 12?
yes
Is your number 13?
yes
13 is the answer. (computer declares final answer)
Thank you for playing the guessing game.

相比之下,我的示例输出为:

Is your number greater than 50?
no
Is your number greater than 25?
no
Is your number greater than 13?
no
Is your number greater than 7?
yes
Is your number greater than 10?
yes
Is your number greater than 11?
yes
Is your number greater than 12?
yes
Is your number 12?
yes
12 is the answer.
Thank you for playing the guessing game.

根据我所做的编辑有一些变化。

代码如下:

//import statements
import java.util.Scanner;
import java.util.ArrayList;
public class Numbers
{


//constant to initialize the ArrayList
private final int AT_MOST = 100;
//anArrayList of type ArrayList<Integer> which is to hold the values from 1 - 100 
private ArrayList<Integer> anArrayList;


/**
 * Constructor of the Numbers() class which initializes all of the instance fields
 */
public Numbers()
{
    anArrayList = new ArrayList<Integer>();
    int i =0;
    //while loop to initialize anArrayList with values from 1-100
    while(i < AT_MOST)
    {
        anArrayList.add(i+1);
        i++;
    }
}

public void search()
{
    int low = 0;
    int high = anArrayList.size();
    int i = 0;
    int j = 0;
    while(low <= high)
    {
        int mid = (low + high)/2;
        mid = anArrayList.get(mid - 1);
        Scanner in = new Scanner(System.in);
        System.out.println("Is your number greater than " + mid + "?");
        String answer = in.nextLine();
        if(answer.equalsIgnoreCase("yes"))
        {

            low = mid + 1;

        }
        else if (answer.equalsIgnoreCase("no"))
        {

            high = mid - 1;
            low++;
        }
        if(low == high+1)
        {
            Scanner in2 = new Scanner(System.in);
            System.out.println("Is your number " + mid + "?");
            String finalAnswer = in2.nextLine();
            if(finalAnswer.equalsIgnoreCase("yes"))
            {
                System.out.println(mid + " is the answer.");
                System.out.println("Thank you for playing the guessing game.");
                low = high + 1;;
            }
            else
            {
                System.out.println("Please play again, something went wrong!");
                low = high + 1;
            }
        }
    }
}
}

当然,这也有一个相对较短的测试器类:

 public class NumbersGuesser
 {
  public static void main(String[] args)
   {
    //creates a new numbers object
    Numbers newNumber = new Numbers();
    //run method is called, game is played.
    newNumber.search();
    }
}
4

1 回答 1

0

既然你努力解决了这个问题,我就继续重组你的 Numbers 类。

我做的第一件事是摆脱 ArrayList。您可以像遍历 ArrayList 一样轻松地对整数进行算术运算。

我添加了几个整数、距离和方向。每次猜测后,距离减半。计算机猜测高或低,直到距离减少到零。在这一点上,这个数字介于低和高之间,包括在内。

方向只是告诉我们下一次猜测是否需要猜测更低(-1)或更高(+1)。

我将高低扫描仪代码拉到自己的方法中。起初它看起来令人困惑,但它所做的只是告诉我们是猜测更高(真)还是更低(假)。通过将此代码移动到它自己的方法中,我可以专注于猜测逻辑。

最后,我在处理结束时关闭了扫描仪。

//import statements
import java.util.Scanner;

public class Numbers {

    // constants to start the game
    private final int AT_LEAST = 0;
    private final int AT_MOST = 100;

    /**
     * Constructor of the Numbers() class
     */
    public Numbers() {

    }

    public void search() {
        int low = AT_LEAST;
        int high = AT_MOST;
        int guess = (low + high) / 2;
        int distance = guess / 2;
        int direction = 1;

        System.out.println("Guess a number between " + low + " and " + high
                + ".");

        Scanner in = new Scanner(System.in);

        do {
            boolean greaterThan = getHighLowResponse(in, direction, guess);
            if (greaterThan) {
                low = guess;
                guess += distance;
                direction = 1;

            } else {
                high = guess;
                guess -= distance;
                direction = -1;
            }
            distance /= 2;
        } while (distance != 0);

        for (int i = low; i <= high; i++) {
            System.out.println("Is your number " + i + "?");
            String finalAnswer = in.nextLine().toLowerCase();
            if (finalAnswer.equalsIgnoreCase("yes")) {
                System.out.println(i + " is the answer.");
                System.out.println("Thank you for playing the guessing game.");
                break;
            }
        }

        in.close();

    }

    private boolean getHighLowResponse(Scanner in, int direction, int guess) {
        do {
            System.out.println("Is your number " + getDirection(direction)
                    + " than " + guess + "?");
            String answer = in.nextLine().toLowerCase();
            if (direction < 0) {
                if (answer.equals("yes"))
                    return false;
                if (answer.equals("no"))
                    return true;
            } else {
                if (answer.equals("yes"))
                    return true;
                if (answer.equals("no"))
                    return false;
            }
        } while (true);
    }

    private String getDirection(int direction) {
        if (direction < 0) {
            return "less";
        } else {
            return "greater";
        }
    }
}
于 2013-03-28T02:03:54.043 回答