java 要求用户在为输入提供错误值后再次输入输入。输入不匹配异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25002802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:08:12  来源:igfitidea点击:

Asking user to enter the input again after he gives a wrong value for the Input. InputMismatchException?

javaexception-handlingtry-catchinputmismatchexception

提问by Anshuman Tripathy

I have created the following class for Inputting a user's age and then displaying appropriate info in the console.

我创建了以下类用于输入用户的年龄,然后在控制台中显示适当的信息。

On running this program , the console asks "Please Enter your Age : "

运行此程序时,控制台会询问“请输入您的年龄:”

If the user enters an Integer for eg: 25 , the executed class displays " Your age is : 25" in the console.

如果用户输入一个整数,例如: 25 ,则执行的类在控制台中显示“您的年龄是:25”。

If the user enters a non-integer number , the console displays: Age should be an Integer Please Enter your Age:

如果用户输入一个非整数 number ,控制台会显示: Age should be an Integer Please Enter your Age:

But I am not able to enter anything through the keyboard when I place my cursor next to "Please Enter your Age: ".

但是当我将光标放在“请输入您的年龄:”旁边时,我无法通过键盘输入任何内容。

I want the user to be able to enter his age again, & if he enters an integer it displays the proper output but if he enters a non-integer the console should ask him again for the age.

我希望用户能够再次输入他的年龄,如果他输入一个整数,它会显示正确的输出,但如果他输入一个非整数,控制台应该再次询问他的年龄。

If you look at my code I'm setting the value of the variable 'age' by calling the function checkAge() inside the else block in my main function.

如果您查看我的代码,我将通过在主函数的 else 块内调用函数 checkAge() 来设置变量“age”的值。

Can anybody tell me where I am going wrong?

谁能告诉我我哪里出错了?

public class ExceptionHandling{

    static Scanner userinput = new Scanner(System.in);

    public static void main(String[] args){ 
        int age = checkAge();

        if (age != 0){
            System.out.println("Your age is : " + age);         
        }else{
           System.out.println("Age should be an integer");
           age = checkAge();
        }
    }

    public static int checkAge(){
        try{            
            System.out.print("Please Enter Your Age :");
            return userinput.nextInt();
        }catch(InputMismatchException e){
            return 0;
        }
    }
}

回答by Eran

You should put your code in a loop if you wish it to execute multiple times (until the user inputs a valid age) :

如果您希望代码执行多次(直到用户输入有效年龄),您应该将代码放入循环中:

public static void main(String[] args)
{
    int age = checkAge();
    while (age == 0) {
       System.out.println("Age should be an integer");
       userinput.nextLine();
       age = checkAge();
    }

    System.out.println("Your age is : " + age);
}

回答by Rod_Algonquin

problem:

问题:

return userinput.nextInt();

By the time you input a sequence of string it will not consume your new line character, and when you go again inside your method and call userinput.nextInt()it will consume that new line and skip it thus not letting you get input again.

当你输入一个字符串序列时,它不会消耗你的换行符,当你再次进入你的方法并调用userinput.nextInt()它时,它会消耗该新行并跳过它,因此不会让你再次输入。

solution:

解决方案:

add nextLine();before you call the checkAgemethod again to consume the new linefrom the string

添加nextLine();之前checkAge再次调用该方法以使用new line字符串中的

sample:

样本:

    System.out.println("Age should be an integer");
    userinput.nextLine();
    age = checkAge();