二进制搜索功能未结束

时间:2018-11-16 14:30:50

标签: java search binary

import java.util.Scanner;

public class GuessingGame {

private Scanner reader;

public GuessingGame() {
    // use only this scanner, othervise the tests do not work
    this.reader = new Scanner(System.in);
}

public void play(int lowerLimit, int upperLimit) {
    instructions(lowerLimit, upperLimit);
    boolean isAboveAverage;
    int counter = -1;

    while (counter < howManyTimesHalvable(upperLimit - lowerLimit)) {
        isAboveAverage = isGreaterThan(average(lowerLimit, upperLimit));
        if (upperLimit == lowerLimit) {
            break;
        }
        if (isAboveAverage) {
            lowerLimit = average(lowerLimit, upperLimit);
        } else {
            upperLimit = average(lowerLimit, upperLimit);
        }
        counter++;
    }

    System.out.println("your number is " + upperLimit);
}

// implement here the methods isGreaterThan and average
public boolean isGreaterThan(int number) {
    boolean isGreater = false;
    boolean isCorrectAnswerGiven = false;

    while (!isCorrectAnswerGiven) {
        System.out.println("Is your number greater than " + (number) + "? (y/n)");
        String answer = reader.nextLine();

        if (answer.equals("yes") || answer.equals("y")) {
            isGreater = true;
            isCorrectAnswerGiven = true;
        } else if (answer.equals("no") || answer.equals("n")) {
            isCorrectAnswerGiven = true;
        }
    }

    return isGreater;
}

public int average(int upperLimit, int lowerLimit) {
    return (upperLimit + lowerLimit) / 2;
}

public int average2(int firstNumber, int secondNumber) {
    double res = (firstNumber + secondNumber) / 2.0;
    Math.round(res);
    //System.out.println(res);
    return (int) res;
}

public void instructions(int lowerLimit, int upperLimit) {
    int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit);

    System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + ".");

    System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions.");
    System.out.println("");
    System.out.println("Next I'll present you a series of questions. Answer them honestly.");
    System.out.println("");
}

// a helper method:
public static int howManyTimesHalvable(int number) {
    // we create a base two logarithm  of the given value

    // Below we swap the base number to base two logarithms!
    return (int) (Math.log(number) / Math.log(2)) + 1;
 }
}

我在调试此代码时遇到问题。在本课程中,我应该实现一个简单的AI,该AI根据您对“您的数字是否比数字高” +数字的问题的答案来猜测一个数字。我的问题是代码永远不会达到给定范围的末尾(因此,范围从1到10,就永远不会猜到1或10)和b。是不会及时停止的。 Java通常会多次重复一个问题,但是程序应该尽快知道答案。

我的错很明显是在播放方法上(为了完整起见,我把整个课程都包括在内),我只是不知道在哪里。我的程序正常运行,因此不太可能出现错字或编程错误。我必须在逻辑上做错事,但我不知道该怎么办。有人知道错误在哪里吗?

1 个答案:

答案 0 :(得分:0)

您的逻辑存在一些问题:

  • 您在每次迭代中都在计算howManyTimesHalvable。考虑到counter的增加,这没有任何意义。它的最后一个可能值始终为1,该值可能远低于当前的counter
  • 您在询问问题后立即检查上和下是否相同,而不更新这些值。只需将其移至循环末尾即可。
  • 最后一件事是,在更新lowerLimit时,您需要将平均值增加1。如果“是否您的电话号码大于X”的答案为是,则新的lowerLimit不能为X

正确的方法:

public void play(int lowerLimit, int upperLimit) {
    instructions(lowerLimit, upperLimit);
    boolean isAboveAverage;
    int counter = howManyTimesHalvable(upperLimit - lowerLimit);
    while (counter > 0) {
        isAboveAverage = isGreaterThan(average(lowerLimit, upperLimit));
        if (isAboveAverage) {
            lowerLimit = average(lowerLimit, upperLimit)+1;
        } else {
            upperLimit = average(lowerLimit, upperLimit);
        }
        if (upperLimit == lowerLimit) {
            break;
        }
        counter--;
    }
    System.out.println("your number is " + upperLimit);
}