Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-当用户在没有输入int数据的情况下按enter键时没有提示 publicstaticintintinput(字符串提示,字符串错误){ int intInput=0;//要返回的变量 //启动扫描仪 扫描仪键盘=新扫描仪(System.in); //此循环检查以确保用户输入了正数据 做{ System.out.println(提示); //此循环检查用户是否输入了int 而(!keyboard.hasNextInt()){ 系统输出打印项次(错误); 键盘。下一步(); } intInput=keyboard.nextInt(); }while(intInput_Java - Fatal编程技术网

Java-当用户在没有输入int数据的情况下按enter键时没有提示 publicstaticintintinput(字符串提示,字符串错误){ int intInput=0;//要返回的变量 //启动扫描仪 扫描仪键盘=新扫描仪(System.in); //此循环检查以确保用户输入了正数据 做{ System.out.println(提示); //此循环检查用户是否输入了int 而(!keyboard.hasNextInt()){ 系统输出打印项次(错误); 键盘。下一步(); } intInput=keyboard.nextInt(); }while(intInput

Java-当用户在没有输入int数据的情况下按enter键时没有提示 publicstaticintintinput(字符串提示,字符串错误){ int intInput=0;//要返回的变量 //启动扫描仪 扫描仪键盘=新扫描仪(System.in); //此循环检查以确保用户输入了正数据 做{ System.out.println(提示); //此循环检查用户是否输入了int 而(!keyboard.hasNextInt()){ 系统输出打印项次(错误); 键盘。下一步(); } intInput=keyboard.nextInt(); }while(intInput,java,Java,您可以在循环中输入一个try…catch,如果用户没有输入int,则只捕获异常: // Method for user int input public static int intInput(String prompt, String error) { int intInput = 0; // Variable to be returned // Start the scanner Scanner keyboard = new Scanner(System.in)

您可以在循环中输入一个
try…catch
,如果用户没有输入
int
,则只捕获异常:

// Method for user int input
public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!(keyboard.hasNextInt())) {
            System.out.println(error);
            keyboard.nextLine(); 
        }

        intInput = keyboard.nextInt();
        keyboard.nextLine();
    } while (intInput <= 0);

    // Returns an int
    return intInput;

}

不过,您可能希望执行一些适当的异常处理,而不是捕获一般的
异常
,但这更像是一个指导原则。通过在
catch
中强制
intInput
0
,这将意味着它将不会中断
do…while
并一直运行,直到输入有效的
int

这是因为“next()”返回下一个标记,跳过任何前导空格,例如换行符如果您想要行处理,而不是令牌处理。另请参见:我仍然会遇到同样的问题我很困惑为什么您要在此处传递
错误
;通常,这将由函数本身引发。@JoshKatofsky
错误
参数是当用户输入非整数时显示给用户的文本,因此消息可以是定制。现在,您阅读了我提供的链接了吗?它是关于将
nextLine()
nextInt()
混合使用的陷阱,您正好遇到了。
public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!keyboard.hasNextInt()) {
            System.out.println(error);
            keyboard.nextLine(); 
        }

        intInput = keyboard.nextInt();

    } while (intInput <= 0);

    // Returns an int
    return intInput;

}
// Method for user int input
public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!(keyboard.hasNextInt())) {
            System.out.println(error);
            keyboard.nextLine(); 
        }

        intInput = keyboard.nextInt();
        keyboard.nextLine();
    } while (intInput <= 0);

    // Returns an int
    return intInput;

}
public static int intInput(String prompt, String error) {

    int intInput = 0;
    Scanner sc = new Scanner(System.in);
    do {
        try {
            System.out.println("Enter an int");
            intInput = Integer.parseInt(sc.nextLine());
        } catch (Exception e) {
            System.out.println(error);
            intInput = 0;
        }
    } while (intInput == 0);

    return intInput;
}