如何使用二分查找找到用户编号

时间:2016-11-12 23:13:13

标签: java binary-search

提示:玩家选择范围(最小值和最大值),然后考虑该范围内的数字(无需在程序中键入数字)。游戏应该使用二分搜索系统地猜测玩家的号码。玩家应该在轮次之间告诉计算机“太高”或“太低”或“正确”。该程序应该继续,直到计算机得到答案,或检测到作弊(或肯定知道答案)。在退出之前,计算机应该说出它有多少“轮次”(它需要多少次猜测)。

问题:第一次计算机出错后,用户声明太高或太低,我无法重新设置上下范围的值

import java.util.Scanner;

public class TestPractice {

    public static void main(String[] args) {
        System.out.println("Think of a number");
        Scanner scan = new Scanner(System.in);
        String x = null;
        String y = null;
        String i = null;
        //Get the input from the player
        System.out.println("Please your maximum value");

        if (scan.hasNext()) {
            x = scan.next();
        }

        System.out.println("Please input your min value");
        if (scan.hasNext()) {
            y = scan.next();
        }

        //Parse the input so its usuable in the array
        int max = Integer.parseInt(x);
        int min = Integer.parseInt(y);

        boolean numberguessed = true; 
        int numberofRounds = 0;

        while(numberguessed) {
            int midpoint = (max+min)/2;

            numberofRounds++;

            System.out.println("Is your number " + midpoint + " please say too low or too high or correct");
             if (scan.hasNext()) {
                 i = scan.next();
             }
             if (i.equalsIgnoreCase("too high")) {
                 min = midpoint;
             }
             if (i.equalsIgnoreCase("too low")) {
                 max = midpoint;
                 min = 0;
             }
             if (i.equalsIgnoreCase("correct")) {
                 System.out.println("the number of rounds in this game is" + numberofRounds);
                 break;
             }

        }

    }
}

1 个答案:

答案 0 :(得分:1)

您需要使用scan.nextLine()代替scan.next()来阅读该行中的所有内容,包括space个字符,这就是为什么max和min从未在第一时间设置的原因。

  

扫描程序使用分隔符模式将其输入分解为标记,分隔符模式默认匹配空格。

More info on scanner

要再次循环整个游戏,请查看do {} while(true);迭代。

System.out.println("Think of a number");
Scanner scan = new Scanner(System.in);
String playAgain = "y";
String x = null;
String y = null;
String i = null;

do {
    // Get the input from the player
    System.out.println("Please your maximum value");

    if (scan.hasNext()) {
        x = scan.next();
    }

    System.out.println("Please input your min value");
    if (scan.hasNext()) {
        y = scan.next();
    }

    // Parse the input so its usuable in the array
    int max = Integer.parseInt(x);
    int min = Integer.parseInt(y);
    int midpoint = 0;
    boolean numberguessed = true;
    int numberofRounds = 0;

    while (numberguessed) {         
        midpoint = (max + min) / 2;
        numberofRounds++;
        System.out.println("Is your number " + midpoint
                + " please press (l) for too low or (h) for too high or (c) for correct");
        if (scan.hasNext()) {
            i = scan.nextLine();
        }
        System.out.println(i);
        if (i.equalsIgnoreCase("h")) {
            min = midpoint;
        } else if (i.equalsIgnoreCase("l")) {
            max = midpoint;
            min = 0;
        } else if (i.equalsIgnoreCase("c")) {
            System.out.println("the number of rounds in this game is"
                    + numberofRounds);
            break;
        }

    }
    System.out.println("Press y to play again");
    if (scan.hasNext()) {
        playAgain = scan.next();
    }
    System.out.println("Game over");
} while (playAgain.equalsIgnoreCase("y"));

More info on do while

建议使用简单的是/否答案,如h,l和c,而不是要求用户写一个单词。请告诉我们。