Java 用户输入法

Java 用户输入法,java,Java,我对这个很陌生。我在创建用户输入法时遇到问题。我想要的基本上只是一个存储用户输入并在输入字母“o”时退出的方法。输入应该是整数,但如果用户输入字符或浮点数,我不希望程序崩溃。到目前为止,我得到的是: public static int userInput() { int number = 0; String r2 = ""; System.out.println("enter number: "); while(input.hasNextInt() == t

我对这个很陌生。我在创建用户输入法时遇到问题。我想要的基本上只是一个存储用户输入并在输入字母“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;      
}
谢谢你给我的任何帮助


提前谢谢

我已经重写了您的代码并添加了注释,解释了您在以下方面遇到的问题:

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;
}

希望这能有所帮助。

此代码存在多个问题。什么是
输入
?您将其视为一个
扫描器
对象,但您在代码或问题中都没有声明这一点。此外,未声明变量
radie
。这应该被编译器捕获;你可能想把它改成
number
。这很有帮助,谢谢!与以前相比,现在的问题是,只要我输入一个数字,程序就会终止。我希望它一直持续下去,直到你按下QT键。这很有帮助,谢谢!现在的问题是,只要我输入一个数字,程序就会终止。我试着将“保持”改为“真”,如果它是一个数字并且似乎有效的话。我现在看到的唯一问题是,如果我在一行中输入例如“1234”,它表示无效数字,我希望它返回1到4。这将有点困难,您可能希望创建另一个函数来进行比较,并返回一个int作为状态码,因为在这种情况下需要区分三种状态:字母、数字、数字。在这种情况下,try/catch不能为您做任何事情,您必须更改代码的该部分,并编写一些内容来解析处理部分中的空格分隔整数(
str.split(“”)
,返回空格分隔的字符串数组)。谢谢!我只是在为java初学者做一个练习,所以我认为它不必这么高级!