用户输入法

时间:2017-03-21 18:39:53

标签: java

我对此非常陌生。我在创建用户输入法时遇到问题。我想要的基本上只是一种存储用户输入的方法,如果有一个字母" o"进入。输入应该只是整数,但如果用户输入字符或浮点数,我不希望程序崩溃。这是我到目前为止所得到的:

public static int userInput() 
{
    int number = 0;

    String r2 = "";
    System.out.println("enter number: ");

    while(input.hasNextInt() == true ){
        number = input.nextInt();
        if(r2.equals("Q") || r2.equals("q")){
            return Integer.MAX_VALUE;}
    }

    while(input.hasNextInt() != true ){         
        r2 = input.next();
        if(r2.equals("Q") || r2.equals("q")){
            return Integer.MAX_VALUE;}

    }
    return number;      
}

感谢我能得到的任何帮助,

提前致谢。

1 个答案:

答案 0 :(得分:1)

我已经重写了您的代码并添加了解释您遇到问题的区域的评论:

public static int userInput() {
    int number = Integer.MAX_VALUE;
    /*
    * In this case, we want some value that indicates when the user decides not
    * to give input (typing "q")
    */
    String r2 = "";
    System.out.println("enter number: ");

    //we have to make input a Scanner object pointed at your input source
    Scanner input = new Scanner(System.in);

    //The loops could be restructured into a single loop:
    boolean stay = true; //a variable to control the loop

    while (stay) {
        /*
        * In most cases, " x == true " is a redundant statement.
        * The comparison evaluates to a boolean, and if the first item
        * is already a boolean (or returns one), there is no need
        * to compare to true.
        */

        /*
        * For a slightly simpler way, simply get whatever the user inputs
        * and store that in a string. There's a way to test whether a
        * String contains characters that can be parsed into an int, as
        * well as a method to do such. We'll use r2 because you already
        * declared that as a String.
        */
        r2 = input.nextLine();
        boolean isNumber;

        //test whether r2 contains only number characters using try / catch
        try {
            Integer.parseInt(r2);
            isNumber = true;
        }
        catch(NumberFormatException e) {
            isNumber = false;
        }

        /*
        * Now that that's been figured out, we run it through our tests
        * (converting to int if that's the case) and take action
        */
        if(isNumber) {
            number = Integer.parseInt(r2); //this will be returned
            stay = false;//no need to keep looping, we got what we came for.
        }
        else if(r2.toLowerCase().matches("q")) { //r2 is not int
            stay = false;// exit loop, Integer.MAX_VALUE will be returned
            //the code calling this function should be prepared to handle
            //that as a special case
        }
        else {
            //if it gets here, the input is neither an int nor "Q" nor "q"
            //better tell them to try again.
            System.out.println("Input invalid, try again.");
            System.out.println("enter number:");
        }


    }
    return number;
}

希望这有帮助。