0

我有应该猜测用户号码的代码,它将根据用户输入缩小搜索范围。唯一的问题是在 while 循环中,条件不适用于 .equals。相反,即使我输入“小于”,它也会跳到 else。这是我下面的代码,我是java新手,所以我可能犯了一个错误。

package reversedHiLo;
//Import utility
import java.util.*;

public class ReversedHiLo 
{

public static void main(String[] args) 
    {
        //create scanner class
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to reverse number guessing game, pick a number between 1 and 100 and type it below:");
        int answer = sc.nextInt();
        //Create the first guess
        int guess = 1 + (int)(100*Math.random());
        //Create an array that stores the range of the player's number
        int[] range = new int[] {1,100};
        //While loop that guesses the number
        while(guess != answer)
        {
            System.out.println("Is your number greater than or less than " + guess + "?" + Arrays.toString(range));
            String response = sc.next();
            sc.nextLine();
            //Conditionals to set the range of the guess
            if(response.equals("less than"))
            {
                range[1] = guess;
            }
            else
            {
                range[0] = guess;
            }
            //Guess a new number based on the range
            guess = range[0] + (int)((range[1] - range[0]) * Math.random());            
        }

        //Final print
        System.out.println("Your number was " + answer + ".\nThe computer's guess was: " + guess);
        //Close scanner
        sc.close();
    }

}
4

1 回答 1

1

有两个地方有问题:

  • 第一个sc.nextInt()方法 - 仅通过将当前读取缓冲区保持在同一行来读取 int 值。因此,要忽略/跳过输入行上int之后的所有内容(如果您只输入数字,则可能是 \n 或 \r\n),您必须使用sc.nextLine()
  • 第二个是sc.next()方法 - 它只从您的行中读取第一个标记(或简单的单词)。这可能就是为什么您只会将“更少”值分配给响应 并且永远不会.equals“小于”。因此,您必须将 sc.next() 替换sc.nextLine ()并从下一行中删除不必要的sc.nextLine() 。

希望现在应该清楚这一点,并且您对调用这些函数时会发生什么有更好的理解。如果没有,那么我强烈建议您查看 Scanner 类,阅读JavaDocs并围绕它编写多个测试,以更好地了解正在发生的事情。

如果我的解释仍然不清楚,请查看下面我为您修改的代码:

public static void main(String[] args)
{
    //create scanner class
    Scanner sc = new Scanner(System.in);
    System.out.println("Welcome to reverse number guessing game, pick a number between 1 and 100 and type it below:");
    int answer = sc.nextInt();
    sc.nextLine();   // This one is necessary to ignore everything on the same line as your number was typed in
    //Create the first guess
    int guess = 1 + (int)(100*Math.random());
    //Create an array that stores the range of the player's number
    int[] range = new int[] {1,100};
    //While loop that guesses the number
    while(guess != answer)
    {

        System.out.println("Is your number greater than or less than " + guess + "?" + Arrays.toString(range));
        String response = sc.nextLine();  // This reads the whole input line

        //Conditionals to set the range of the guess
        if(response.equals("less than"))
        {
            range[1] = guess;
        }
        else
        {
            range[0] = guess;
        }
        //Guess a new number based on the range
        guess = range[0] + (int)((range[1] - range[0]) * Math.random());
    }

    //Final print
    System.out.println("Your number was " + answer + ".\nThe computer's guess was: " + guess);
    //Close scanner
    sc.close();
}
于 2018-10-19T16:45:56.923 回答