【发布时间】:2018-08-14 10:09:57
【问题描述】:
我有一个关于如何在开关盒内执行多项检查的问题?我需要在案例 2 中进行一些检查,但是添加第二个 if 块,我的应用程序什么也不做,它只是挂起。我做错了什么?
BufferedReader inputCommand = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Instruction:");
System.out.println();
System.out.println("1 -- Show all product at the store");
System.out.println("2 -- Add the product at the client basket");
System.out.println("3 -- Show client basket");
System.out.println();
switch (inputCommand.readLine()) {
case "1":
basketCommand.get();
System.out.println();
break;
case "2":
System.out.println();
System.out.println("Select product to add into your basket");
if (inputCommand.readLine().equals("su")){
basketCommand.addIntoBasket(productContainer.productList.get("su"));
}
if (inputCommand.readLine().equals("an")){
basketCommand.addIntoBasket(productContainer.productList.get("an"));
}
break;
}
【问题讨论】:
-
一般你不应该在
switch语句中执行复杂的操作,你应该创建一个新的方法;在这种情况下selectProduct浮现在脑海中。
标签: java switch-statement case