输入必须输入两次,并且在可以输入字符串时不允许输入

时间:2018-11-12 19:54:13

标签: java methods user-input

我的代码

package morrisJCh5Sec2;

import java.util.Scanner;

public class Ch5Sec2 {
    public static int collectInteger(String purpose, int minimum, int maximum) {

        Scanner input = new Scanner(System.in);
        System.out.println(purpose);
        System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
        int value = input.nextInt();
        while(input.hasNext()) {

            if(!input.hasNextInt()) {
                System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                value = input.nextInt();
                input.next();
                continue;
                //not an integer
            }//end not int if
            else {
                value = input.nextInt();
                if(value >= minimum && value <= maximum) {
                    return value;
                }
                else {
                    System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                    System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                    value = input.nextInt();
                }//end else out of bounds

            }
            //input.close();
        }

        return 0;
    }//end collectInteger
    public static void main(String args[]) {
        final int LOW_INT = 0;
        final int HIGH_INT = 100;
        int inputValue = collectInteger("Enter the number of cats.", LOW_INT, HIGH_INT);
        System.out.printf("Your number of cats was %d.\n", inputValue);
    }//end main
}//end class

我的输出:

Enter the number of cats.
    Enter an integer between 0 and 100: -56
-56
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 101
101
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: jads
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at morrisJCh5Sec2.Ch5Sec2.collectInteger(Ch5Sec2.java:30)
    at morrisJCh5Sec2.Ch5Sec2.main(Ch5Sec2.java:42) 

我的输出应允许输入字符串,并且它将再次要求输入整数。另一个问题是,我的代码不应让用户在其输入中键入两次,而应仅使用户输入一次并运行代码。

1 个答案:

答案 0 :(得分:2)

您的代码中还有许多对next()nextInt()的调用。您可以将代码转换为do-while循环。另外,您在第一个next()中还有一个额外呼叫if的电话。然后在内部else中要求输入nextInt()的用户输入,但是由于在循环开始时调用了nextInt(),所以这是多余的,这就是为什么它要求输入的原因两次。同样在您的第一个if中,它处理输入是否不是可解析的int。如果不是,则您有value = input.nextInt(),这将导致输入不匹配异常。而是调用空白的next()来清除错误的输入:

public static int collectInteger(String purpose, int minimum, int maximum) {

        Scanner input = new Scanner(System.in);
        System.out.println(purpose);
        System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
        int value;
        do {    
            if(!input.hasNextInt()) {
                System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");
                input.next();    //clear bad input                                                     
            }
            else {
                value = input.nextInt();
                if(value >= minimum && value <= maximum) {
                    return value;
                }
                else {
                    System.out.println("The value you enter needs to be between " + minimum + " and " + maximum + ". Please try again.");
                    System.out.printf("\tEnter an integer between " + minimum + " and " + maximum + ": ");

                }

            }

        } while(input.hasNext());
        return 0;
}

示例输出:

Enter the number of cats.
Enter an integer between 0 and 100: -51
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 20000
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: Hello
The value you enter needs to be between 0 and 100. Please try again.
    Enter an integer between 0 and 100: 4
Your number of cats was 4.