如何匹配Java计算机猜测您的数字游戏的给定输出;二进制搜索

时间:2013-03-26 10:20:45

标签: java

我一直遇到很多麻烦,很快就要到了,我想知道是否有人知道如何解决我的问题。我必须创建一个程序,其中: “你的任务是实现一个基于二进制搜索原理的数字猜测器。在每一步中,计算机将查询间隔减半。当间隔包含一个数字时,它会宣告答案。程序的用户选择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();
    }
}

1 个答案:

答案 0 :(得分: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";
        }
    }
}