【发布时间】:2015-05-12 14:54:42
【问题描述】:
System.out.println("\nEnter item's price");
Scanner newItemPriceSC = new Scanner(System.in);
Double newItemPrice = newItemPriceSC.nextDouble();//stores item price
String goBack = newItemPriceSC.nextLine();
System.out.println("type \"no more\" if there are no more items\ntype any other word to continue");
String answ = continueEnd.nextLine();
if(!(answ.equals("no more"))){
continue;//if user does not answer "no more!", loop continues
}
if(answ.equals("no more") || goBack.equals("no more")){//if user answers "no more!":
最后一段代码:
goBack.equals("no more")
不触发 if 语句的内容(未显示),当我键入“no more”时显示以下错误文本:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Ben_Li_CashRegisterProgram.main(Ben_Li_CashRegisterProgram.java:64)
我将上面的 goBack 声明为一个字符串,它存储下一个用户输入的字符串的内容,该字符串将使用 newItemPriceSC 进行扫描。我使用相同的扫描仪扫描 newItemPrice,一个 double,它可以正常执行。
注意 if 语句的第一部分确实执行了 if 语句的内容:
(answ.equals("no more")
【问题讨论】:
-
您在运行此程序时发现了什么?您是否在运行时检查了变量以查看它们所包含的内容?
-
@Patrick,当打印“输入商品价格”时,我输入“不再”。然后它将显示“主线程中的错误”以及另外几行错误文本。我没有在运行时检查变量
-
我相信在问题中添加错误文本将有助于社区帮助您了解您的问题。很难从“它没有按照我想要的那样工作”来猜测错误。
标签: java string if-statement while-loop java.util.scanner