繁体   English   中英

Java验证用户整数输入

[英]Java validating user integer input

我写的代码

1)不断要求用户选择数字并检查输入是否为整数

private static int readInputInt(String error) {
    Scanner s = new Scanner(System.in);
    while (!s.hasNextInt()) {
        System.out.println(error);
        s.next();
    }
    int result = s.nextInt();
    //s.close(); 
    return result;
}

2)连续要求用户选择范围内的数字并仅显示错误,程序不显示消息要求用户再次提供输入。

private static int readInputInt(String error, int max) {
    Scanner s = new Scanner(System.in);
    int result;
    do {
        while(!s.hasNextInt()) {
            //show error if it is not integer and get input once again
            System.out.println(error);
            s.next();
        }
        result = s.nextInt();
        // if result is integer check if it is bigger than max
        if(result > max) {
            System.out.println(error);
        }

    }while(result > max);

我想知道是否有任何更简单的方法可以做到这一点,因为我花了太多时间来做这件事,我认为这是垃圾的编码方式。

首先,我坚信以下代码将起作用:

private static int readInputInt(String error, int max) {
    Scanner s = new Scanner(System.in);
    while (!s.hasNextInt() && (s.nextInt < max)) {
        System.out.println(error);
        s.next();
    }
    int result = s.nextInt();
    //s.close(); 
    return result;
}

但这不起作用。 任何帮助,谢谢!

我在编写代码时遇到了几次这个问题,而最适合我的解决方案是使用

试着抓

让我告诉你我的意思。

private static int readInputInt(String error) {
        Scanner s = new Scanner(System.in);

        //1. Reading the input as a string.
        String input = s.nextLine();

        int result;

        // A while loop that will keep going on until the user enters a 
        // valid integer.
        while (true) {

           // Try/catch block that tries to parse the string to an integer
           try {

               // If the user enters a valid integer there will be no problem 
               // parsing it. Otherwise, the program will throw a 'NumberFormatException'.
               result = Integer.parseInt(input);


               // If the parsing has been successful, 
               //we break the while loop and return the result
               break;
           }catch(NumberFormatException nfe) {

               // If the user did not enter a valid integer we will just 
               // print the error message.
               System.out.println(error);
           }

           // Read user input again and repeat the procedure above.
           input = s.nextLine();
        }
        return result;
    }

希望对您有所帮助。 如果您不熟悉try / catch ,建议您在线阅读。 整齐!

private static int readInputInt(String error, int max) {
    Scanner s = new Scanner(System.in);
    int result;
    while (true) { 
        if (s.hasNextInt()) {
            result = s.nextInt();    
            if (result <= max) {
                s.close();
                return result;
            }
         } else { //Only want to call next() if it doesn't meet the first conditional. We've already called next when it is an int. 
             s.next();
         }
         System.out.println(error);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM