java 如何创建检查 int 范围、数字类型而不是 char 的异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15468418/
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
How can I create an exception that checks for int range, number type, and not a char?
提问by user2180681
I'm trying to wrap my head around exceptions and the problem I'm running into is that I'm being required to create a program that asks for user input for a number 9-99. This number must be error-checked using 3 different exceptions.
我试图解决异常问题,我遇到的问题是我需要创建一个程序,要求用户输入 9-99 的数字。这个数字必须使用 3 个不同的异常进行错误检查。
e1: number is outside of the range (200)
e2: number is of a data type other than integer (double)
e3: input is another data type other than number (char)
e1:数字超出范围 (200)
e2:数字是整数(double)以外的数据类型
e3:输入是除数字(字符)以外的另一种数据类型
I have tried to create patterns in my if structure to make all three work, however I can't get it to differentiate between e2 and e3. It will always default to e2. This is what I have with only two exceptions, but I would greatly appreciate help with figuring out how to implement the third. Thank you.
我试图在我的 if 结构中创建模式以使所有三个工作,但是我无法区分 e2 和 e3。它将始终默认为 e2。这就是我所拥有的,只有两个例外,但我将不胜感激帮助弄清楚如何实现第三个。谢谢你。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean tryAgain = true;
do {
try {
System.out.println("Please enter an integer between 9 and 99: ");
int inInt = input.nextInt();
if (inInt >= 9 && inInt <= 99){
System.out.println("Thank you. Initialization completed.");
tryAgain = false;
}
else if (inInt < 9 || inInt > 99){
throw new NumberFormatException("Integer is out of range.");
}
}
catch (NumberFormatException e1) { // Range check
System.out.println("* The number you entered is not between 9 and 99. Try again.");
System.out.println();
input.nextLine();
}
catch (InputMismatchException e2) { // Something other than a number
System.out.println("* You did not enter an integer. Try again.");
System.out.println();
input.nextLine();
}
} while(tryAgain);
}
}
}
Here is the output I get right now:
这是我现在得到的输出:
Please enter an integer between 9 and 99: 2
- The number you entered is not between 9 and 99. Try again.
Please enter an integer between 9 and 99: f
- You did not enter an integer. Try again.
Please enter an integer between 9 and 99: 88
Thank you. Initialization completed.
请输入 9 到 99 之间的整数:2
- 您输入的数字不在 9 到 99 之间。请重试。
请输入 9 到 99 之间的整数:f
- 您没有输入整数。再试一次。
请输入 9 到 99 之间的整数:88
谢谢你。初始化完成。
回答by Patashu
In catch (InputMismatchException e2)
, test to see if input.hasNextDouble()
(or input.hasNextFloat()
? Not sure which one is more general...) is true. If it is, then you can distinguish between the case 'user entered a double' and 'user entered a non numeric type'
在 中catch (InputMismatchException e2)
,测试以查看input.hasNextDouble()
(或input.hasNextFloat()
?不确定哪个更通用...)为真。如果是,那么您可以区分“用户输入双精度”和“用户输入非数字类型”的情况
回答by Hot Licks
If you've got to detect three circumstances, you need to have three sets of logic.
如果你必须检测三种情况,你需要有三套逻辑。
- Check if the entered characters are a valid numeric value.
- Check that the entered number is an integer.
- Check that the entered number falls between the low and high bounds.
- 检查输入的字符是否是有效的数值。
- 检查输入的数字是否为整数。
- 检查输入的数字是否介于上下限之间。
And you pretty much have to check them in that order.
而且您几乎必须按照该顺序检查它们。
Scanner conveniently has the hasXXX
methods to see whether the characters about to be read match a given pattern.
Scanner 有hasXXX
多种方法可以方便地查看将要读取的字符是否与给定的模式匹配。