【发布时间】:2012-10-07 22:24:20
【问题描述】:
我已经盯着这个看了好几个小时了,想不出解决办法;我通常使用正则表达式处理这种类型的验证,但我尝试使用内置解决方案进行更改(显然,我不经常这样做):
private static double promptUserDecimal(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
try{
double input2 = Double.parseDouble(scan.nextLine());
return input2;
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
}
}
这个错误是编译器找不到“返回”,所以我得到一个编译错误。如果我将“return”放在 try/catch 之外,我需要声明/初始化“input2”,这违背了操作的目的。任何帮助表示赞赏...
【问题讨论】:
-
您捕获了异常,打印出一条消息,但是该方法仍然从
catch块继续。编译器抱怨如果 catch 块被命中,你不会返回(并非所有路径都返回)。
标签: java compiler-errors