【问题标题】:Need to validate int; hasNextInt isn't working需要验证int; hasNextInt 不起作用
【发布时间】:2020-09-05 15:10:36
【问题描述】:

我正在处理创建猜谜游戏的家庭作业问题。我已经让那部分工作了,但我们必须验证输入。我尝试使用 hasNextInt,但一直收到错误消息,提示“无法取消引用 int”并指向“!guess.hasNextInt”代码。

我已经尝试了很多次迭代,但仍然遇到同样的错误。我包含的代码只是我最近的尝试。

如何让 hasNextInt 工作或者我应该如何验证输入?

import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 101);

        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to my Guessing Game!");

        int guess = -1;
        //Loop goes as long as guess doesn't equal num
        while (guess != num) {
            System.out.print("Guess a number between 1 and 100:   ");
            guess = input.nextInt();

            //Validates input
            while (!guess.hasNextInt()) {
                System.out.println("Invalid response, try again.");
                in.next();
            }

            if (guess == num)
                System.out.println("Correct!");
            else if (guess < num)
                System.out.println("Your guess was too low");
            else
                System.out.println("Your guess was too high");

        }
    }
}

【问题讨论】:

  • 变量guess 是基本类型int。因此,您不能在其上调用方法,例如当您尝试调用guess.hasNextInt() 时。 hasNextInt()Scanner 类的一个方法。另请注意,hasNextInt() 不会用于验证输入。
  • .hasNextInt()Scanner 类的方法,您正试图在int 变量上调用它。您可能打算使用input 而不是guessinput.hasNextInt()

标签: java


【解决方案1】:

我修复了代码:

public static void main(String[] args) {
    int num = (int) (Math.random() * 101);
    System.out.println(num);
    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to my Guessing Game!");

    int guess = -1;
    //Loop goes as long as guess doesn't equal num
    while (guess != num) {
        System.out.print("Guess a number between 1 and 100:   ");
        guess = input.nextInt();

        if (guess == num) {
            System.out.println("Correct!");
            break;}
        else if (guess < num)
            System.out.println("Your guess was too low");
        else
            System.out.println("Your guess was too high");

        //Validates input
        while (!input.hasNextInt()) {
            System.out.println("Invalid response, try again.");
            input.next();
        }

       
    }
}

如果用户用break猜到了游戏结束的数字

while (!guess.hasNextInt()) 中,您使用了一个整数,预计扫描器input

【讨论】:

  • 好消息!在查看您的代码之前,我最终自己解决了这个问题。从本质上讲,我们都得到了最终结果,但略有不同。我将把我的代码放在下面:
【解决方案2】:

如果你想解析你的号码,你可以使用 Integer.parseInt() 方法,如图所示。您也错误地使用了 hasNextInt() 。您也没有将 in.next() 值存储在任何变量中。

import java.util.Scanner;

public class GuessNumber {
 public static void main(String[] args) {
    int num = (int)(Math.random() * 101);

    Scanner input = new Scanner(System.in);
    System.out.println("Welcome to my Guessing Game!");

    int guess = -1;
    //Loop goes as long as guess doesn't equal num
    while (true) {
        System.out.print("Guess a number between 1 and 100:   ");
        String numberString = input.nextLine();

        //Validates input
        try {
            guess = Integer.parseInt(numberString);
            if (guess == num) {
                System.out.println("Correct!");
                break;
            } else if (guess < num)
                System.out.println("Your guess was too low");
            else
                System.out.println("Your guess was too high");
        } catch (Exception e) {
            System.out.println("Invalid response, try again.");
        }
       }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多