Java 尝试捕获和用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18668045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Try catch and user input
提问by Stickandjab
This is a question for a homework assignment involving a try/catch block. For a try/catch I know that you place the code you want to test in the try block and then the code you want to happen in response to an exception in the catch block but how can i use it in this particular case?
这是一个涉及 try/catch 块的家庭作业的问题。对于 try/catch,我知道您将要测试的代码放在 try 块中,然后将要发生的代码用于响应 catch 块中的异常,但是在这种特殊情况下我如何使用它?
The user enters a number which is stored in userIn but if he enters a letter or anything besides a number I want to catch it. The user entered number will be used in a switch statement after the try/catch.
用户输入一个存储在 userIn 中的数字,但如果他输入一个字母或除数字之外的任何内容,我想抓住它。用户输入的数字将在 try/catch 之后的 switch 语句中使用。
Scanner in = new Scanner(System.in);
try{
int userIn = in.nextInt();
}
catch (InputMismatchException a){
System.out.print("Problem");
}
switch(userIn){...
When I try to compile, it returns symbol not found, for the line number corresponding to the beginning of the switch statement, switch(userIn){. A few searches later I find that userIn cannot be seen outside the try block and this may be causing the error. How can I test userIn for correct input as well as have the switch statement see userIn after the try/catch?
当我尝试编译时,它返回符号未找到,对于对应于 switch 语句开头的行号,switch(userIn){。几次搜索后,我发现在 try 块之外无法看到 userIn,这可能是导致错误的原因。如何测试 userIn 的正确输入以及在 try/catch 之后让 switch 语句看到 userIn?
采纳答案by Raghav Sood
Use something like:
使用类似的东西:
Scanner in = new Scanner(System.in);
int userIn = -1;
try {
userIn = in.nextInt();
}
catch (InputMismatchException a) {
System.out.print("Problem");
}
switch(userIn){
case -1:
//You didn't have a valid input
break;
By having something like -1
as the default value (it can be anything that you won't receive as input in the normal run, you can check if you had an exception or not. If all ints are valid, then use a boolean flag which you can set in the try-catch blocks.
通过将类似的东西-1
作为默认值(它可以是您在正常运行中不会作为输入接收的任何东西,您可以检查是否有异常。如果所有整数都有效,则使用布尔标志,您可以在 try-catch 块中设置。
回答by Caffe Latte
The int userIn
is inside the try-catch
scope, and you can use it only inside the scope not outside.
该int userIn
是内部的try-catch
范围,不能外面只使用它里面的范围。
You must declare it outside the try-catch
brackets:
您必须在try-catch
括号外声明它:
int userIn = 0;
try{
userIn = ....
}.....
回答by fujy
Try something like this
尝试这样的事情
int userIn = x; // where x could be some value that you're expecting the user will not enter it, you could Integer.MAX_VALUE
try{
userIn = Integer.parseInt(in.next());
}
catch (NumberFormatException a){
System.out.print("Problem");
}
This will cause and exception if the user entered anything than numbers because it will try to parse the user input String
as a number
如果用户输入的不是数字,这将导致异常,因为它会尝试将用户输入解析String
为数字