Java:输入无效数据时重新提示用户输入的更简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16932775/
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-01 00:25:20  来源:igfitidea点击:

Java: Easier Way to Re-prompt user for input when invalid data is entered

java

提问by Singh

I am creating a simple program using the java language which uses a bunch of similar methods to retrieve information from the user. The method i have used to deal with the user entering invalid data seems very incorrect to me, so i am seeking professional guidance on how to better handle invalid data.

我正在使用 java 语言创建一个简单的程序,该程序使用一堆类似的方法从用户那里检索信息。我用来处理用户输入无效数据的方法对我来说似乎很不正确,所以我正在寻求有关如何更好地处理无效数据的专业指导。

I have attempted to search for similar questions but have found none.

我试图搜索类似的问题,但没有找到。

This is a sample of one of my methods:

这是我的一种方法的示例:

public static int getInput()
{
    int temp = 1;

    do
    {
        System.out.println("Answers must be between 1 and 15");
        temp =  reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
        }
    }while(temp > 15 || temp < 1);

    //This value will never be reached because the do while loop structure will not end until a valid return value is determined
    return 1;
}//End of getInput method

Is there a better way to write this method?

有没有更好的方法来编写这个方法?

This question is entirely made up so i can learn a better method to implement in my future programs.

这个问题是完全弥补的,所以我可以学习一种更好的方法来在我未来的程序中实现。

Is using a labeled break statement acceptable? such as:

使用带标签的 break 语句是否可以接受?如:

public static int getInput()
{
    int temp = 1;

    start:

        System.out.println("Answers must be between 1 and 15");
        temp =  reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
            break start;
        }


}

Thank you very much in advance.

非常感谢您提前。

回答by qqilihq

You have forgotten to check the case, that non-number values are entered (Scanner#nextIntthrows a java.util.InputMismatchException). One suggestion which takes care of that issue, is less redundant and more flexible:

您忘记检查大小写,输入了非数字值(Scanner#nextInt抛出 a java.util.InputMismatchException)。解决这个问题的一个建议是不那么多余且更灵活:

public static int getInput(int min, int max) {
    for (;;) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(String.format("Answers must be between %s and %s", min, max));
        try {
            int value = scanner.nextInt();
            if (min <= value && value <= max) {
                return value;
            } else {
                System.out.println("Please enter a valid value");
            }
        } catch (InputMismatchException e) {
            System.out.println("Input was no number");
        }
    }
}

回答by Dan675

If you are just worried about the return that is not used and double checking temp you can do something like

如果您只是担心未使用的退货并仔细检查温度,您可以执行以下操作

public static int getInput()
{
    while(true)
    {
        System.out.println("Answers must be between 1 and 15");
        temp = reader.nextInt();

        if(temp >=1 && temp <= 15)
        {
            return temp;
        }
        else
        {
            System.out.println("Please enter a valid value");
        }
    }
}//End of getInput method