java—需要验证int;hasnextint不起作用

nnsrf1az  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(299)

我正在做一个家庭作业,做一个猜谜游戏。我已经让这部分工作,但我们必须验证输入。我尝试过使用hasnextint,但是我不断得到一个错误,说“int不能被取消引用”,并指向“!猜猜看。有“下一个”代码。
我尝试了很多次迭代,但仍然得到相同的错误。我包含的代码只是我最近的一次尝试。
如何让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");

        }
    }
}
oxiaedzo

oxiaedzo1#

如果您想解析数字,可以使用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.");
        }
       }
    }
}
llew8vvj

llew8vvj2#

我修正了密码:

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();
        }

    }
}

如果用户猜到了游戏结束时使用的数字 breakwhile (!guess.hasNextInt()) 你用的是一个整数,除了扫描器 input

相关问题