Java 在整数中输入小数时打印错误
我正在写一个叫做Revenue的程序,我已经满足了所有的要求,除了一个Java 在整数中输入小数时打印错误,java,int,java.util.scanner,Java,Int,Java.util.scanner,我正在写一个叫做Revenue的程序,我已经满足了所有的要求,除了一个 System.out.print("\t Enter quantity(ies): "); quantity = keyboard.nextInt(); if (quantity < 0 || quantity > 150 || quantity == '.'){ System.out.println(); System.out.println("\t Inv
System.out.print("\t Enter quantity(ies): ");
quantity = keyboard.nextInt();
if (quantity < 0 || quantity > 150 || quantity == '.'){
System.out.println();
System.out.println("\t Invalid item price.");
System.out.println("\t Please run the program again");
System.out.println();
System.out.println("Thank you for using \"Temple\" store");
System.exit(0);
keyboard.nextInt()
将不允许小数。如果您键入类似“3.14.15”的内容,它将抛出异常。对于错误处理,您需要捕获此异常并打印出比默认情况下看到的堆栈跟踪更好的错误消息。使用以下代码:
String quantity = keyboard.next(); // use keyboard.next() instead of keyboard.nextInt()
// because it will throw Exception if value is other
//than int
boolean flag = quantity.matches("[0-9]+");
if (flag) {
if (Integer.parseInt(quantity) < 0
|| Integer.parseInt(quantity) > 150) {
flag = false;
}
}
if (!flag) {
System.out.println();
System.out.println("\t Invalid item price.");
System.out.println("\t Please run the program again");
System.out.println();
System.out.println("Thank you for using \"Temple\" store");
System.exit(0);
}
字符串数量=键盘.next();//使用keyboard.next()而不是keyboard.nextInt()
//因为如果值为其他值,它将引发异常
//than int
布尔标志=数量。匹配(“[0-9]+”);
国际单项体育联合会(旗){
if(Integer.parseInt(数量)<0
||整数.parseInt(数量)>150){
flag=false;
}
}
如果(!标志){
System.out.println();
System.out.println(“\t无效项目价格”);
System.out.println(“\t请再次运行该程序”);
System.out.println();
System.out.println(“感谢您使用\“Temple\”商店”);
系统出口(0);
}
使用try-catch块来完成您想要的:
System.out.print("\t Enter quantity(ies): ");
try{
quantity = keyboard.nextInt();
if (quantity < 0 || quantity > 150)
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
System.out.println();
System.out.println("\t Invalid item price.");
System.out.println("\t Please run the program again");
System.out.println();
System.out.println("Thank you for using \"Temple\" store");
System.exit(-1); //-1 signs an error to the application that launched the program
}
System.out.print(“\t输入数量):”;
试一试{
数量=键盘.nextInt();
如果(数量<0 | |数量>150)
抛出新的IllegalArgumentException();
}捕获(IllegalArgumentException e){
System.out.println();
System.out.println(“\t无效项目价格”);
System.out.println(“\t请再次运行该程序”);
System.out.println();
System.out.println(“感谢您使用\“Temple\”商店”);
System.exit(-1);//-1表示启动程序的应用程序出现错误
}
你的教授会对你的工作感到失望this@getlost我想Kacey想做一个.contains('.')
,但无论如何都不行,因为他的输入是int
而不是字符串
。@Kacey你能用细节编辑你的原始问题吗?特别是,你所说的“如果我键入一个双精度”是什么意思?还有,您得到了什么错误?@Kacey对不起,我对Scanner.nextInt()
的行为的猜测是不正确的。我已编辑了我的答案,并建议如何解决该问题。请使用try catch
。捕获该异常并要求用户重试。@看起来OP应该只打印一条错误消息,如“请再次运行该程序”。为什么不在Catch
中同时使用扫描仪
?这样用户就不必再次运行程序。如果用户键入“abcde”会发生什么情况?@Code学徒:谢谢,我没有检查字符串“!@$%”呢?我们可以继续。我认为一个更简单的解决方案是仍然调用nextInt()
,它将处理许多错误情况(包括我们没有想到的情况),并在错误输入时捕获抛出的异常。@code学徒:我认为这将只过滤数字是的,看起来不错。请注意,您基本上是在重写Scanner.nextInt()
和Integer.parseInt()
的逻辑。
System.out.print("\t Enter quantity(ies): ");
try{
quantity = keyboard.nextInt();
if (quantity < 0 || quantity > 150)
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
System.out.println();
System.out.println("\t Invalid item price.");
System.out.println("\t Please run the program again");
System.out.println();
System.out.println("Thank you for using \"Temple\" store");
System.exit(-1); //-1 signs an error to the application that launched the program
}