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