我在使用这个特定的 try/catch 块时遇到了一些问题。它应该可以工作,但是每次我输入一个字母时,我仍然得到一个NumberFormatException
. 任何人都可以识别此代码的问题吗?
try {
if (convertToNum > 4) {
System.out.println("You entered a number that is too high");
}
else if (convertToNum < 1) {
System.out.println("You entered a number that is too low");
}
else {
System.out.println("You subtracted " + convertToNum +
" from the pile.");
}
}
catch (NumberFormatException e){
System.out.println ("You entered an invalid number");
}
return convertToNum;
}