在While循环中时,等于函数不起作用

时间:2018-10-19 16:11:10

标签: java

我有用于猜测用户号码的代码,它将根据用户输入来缩小搜索范围。唯一的问题是,在while循环中,条件语句不适用于.equals。相反,即使我键入“小于”,它也会跳到其他位置。这是我下面的代码,我是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();
    }

}

1 个答案:

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