Java - 用于扫描程序验证的while循环导致输入失败

时间:2021-02-07 21:50:06

I am trying to add the following scanner validation as follows;

我正在尝试添加以下扫描程序验证,如下所示;

public void promptFilmRating() {
    while (!filmScanner.hasNextInt()) {
        System.out.println("Please enter a number instead of text.");
        filmScanner.next();
    }
    while (filmScanner.nextInt() > 5 || filmScanner.nextInt() < 1) {
        System.out.println("Your number is outside of the rating boundaries, enter a number between 1 and 5.");
        filmRatingOutOfFive = filmScanner.nextInt();
    }

}

However when using the code that relates to the integer between value validation, repeated inputs are needed in order to record the original input and I am unsure on how to correct this, any advice would be fantastic.

但是,当使用与值验证之间的整数相关的代码时,需要重复输入以记录原始输入,并且我不确定如何纠正这一点,任何建议都会很棒。

2 个解决方案

#1


1  

I believe your problem is in while (filmScanner.nextInt() > 5 || filmScanner.nextInt() < 1) {. Every call to filmScanner.nextInt() asks the stream for a new integer, so by calling .nextInt() twice in the while statement, you are asking for two numbers.

我相信你的问题在于(filmScanner.nextInt()> 5 || filmScanner.nextInt()<1){。每次调用filmScanner.nextInt()都要求流输入一个新的整数,所以通过在while语句中调用两次.nextInt(),你需要两个数字。

You might want to consider combining your two loops into one. Example:

您可能需要考虑将两个循环合并为一个循环。例:

int myNum;
do {
    myNumb = filmScanner.nextInt();
} while (myNum > 5 || myNum < 1);

#2


1  

Store the value that you are getting in a variable, and use that variable to perform the checks.

将您获得的值存储在变量中,并使用该变量执行检查。

Here is an example:

这是一个例子:

private void promptFilmRating() {

    Scanner filmScanner = new Scanner(System.in);
    int filmRatingOutOfFive;

    do{
        System.out.println("Please enter your rating for the film:");
        filmRatingOutOfFive = filmScanner.nextInt();

        if(filmRatingOutOfFive > 5 || filmRatingOutOfFive < 1)
            System.out.println("Your number is outside of the rating boundaries, enter a number between 1 and 5.");

    }while(filmRatingOutOfFive > 5 || filmRatingOutOfFive < 1);


    System.out.println("You rated this film: "+filmRatingOutOfFive+" out of 5");


}

#1


1  

I believe your problem is in while (filmScanner.nextInt() > 5 || filmScanner.nextInt() < 1) {. Every call to filmScanner.nextInt() asks the stream for a new integer, so by calling .nextInt() twice in the while statement, you are asking for two numbers.

我相信你的问题在于(filmScanner.nextInt()> 5 || filmScanner.nextInt()<1){。每次调用filmScanner.nextInt()都要求流输入一个新的整数,所以通过在while语句中调用两次.nextInt(),你需要两个数字。

You might want to consider combining your two loops into one. Example:

您可能需要考虑将两个循环合并为一个循环。例:

int myNum;
do {
    myNumb = filmScanner.nextInt();
} while (myNum > 5 || myNum < 1);

#2


1  

Store the value that you are getting in a variable, and use that variable to perform the checks.

将您获得的值存储在变量中,并使用该变量执行检查。

Here is an example:

这是一个例子:

private void promptFilmRating() {

    Scanner filmScanner = new Scanner(System.in);
    int filmRatingOutOfFive;

    do{
        System.out.println("Please enter your rating for the film:");
        filmRatingOutOfFive = filmScanner.nextInt();

        if(filmRatingOutOfFive > 5 || filmRatingOutOfFive < 1)
            System.out.println("Your number is outside of the rating boundaries, enter a number between 1 and 5.");

    }while(filmRatingOutOfFive > 5 || filmRatingOutOfFive < 1);


    System.out.println("You rated this film: "+filmRatingOutOfFive+" out of 5");


}