我对如何在开关盒内执行几项检查有疑问?在情况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;
}
答案 0 :(得分:6)
在第二个case语句中,您应该仅读取下一个输入一次:
{
"type": "Microsoft.Resources/deployments",
"name": "SolutionDeployment",
"apiVersion": "2017-05-10",
"resourceGroup": "[split(variables('omsWorkspaceId'),'/')[4]]",
"subscriptionId": "[split(variables('omsWorkspaceId'),'/')[2]]",
"properties": {
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [
{
"apiVersion": "2017-03-15-preview",
"type": "Microsoft.OperationsManagement/solutions",
"location": "[parameters('workspaceRegion')]",
"name": "[concat('ContainerInsights', '(', split(variables('omsWorkspaceId'),'/')[8], ')')]",
"properties": {
"workspaceResourceId": "[variables('omsWorkspaceId')]"
},
"plan": {
"name": "[concat('ContainerInsights', '(', split(variables('omsWorkspaceId'),'/')[8], ')')]",
"product": "[concat('OMSGallery/', 'ContainerInsights')]",
"promotionCode": "",
"publisher": "Microsoft"
}
}
]
}
},
"dependsOn": [ "[concat('Microsoft.Resources/deployments/', 'WorkspaceDeployment')]" ]
}
答案 1 :(得分:2)
您的第一个if
语句吞噬了输入行,因此第二个if
语句无任何内容。
只需读取一次就存储该行:
String readLine = inputCommand.readLine();
if (readLine.equals("su")){
basketCommand.addIntoBasket(productContainer.productList.get("su"));
}
else if (readLine.equals("an")){
basketCommand.addIntoBasket(productContainer.productList.get("an"));
}
答案 2 :(得分:2)
每次您执行inputCommand.readLine()
时,程序都会等待您身边的输入。您无法像本例中那样比较单个输入。最好的方法是将输入保存在变量中,然后执行检查。这样的事情会起作用(未经测试):
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");
String input = inputCommand.readLine();
if (input.equals("su")){
basketCommand.addIntoBasket(productContainer.productList.get("su"));
}
if (input.equals("an")){
basketCommand.addIntoBasket(productContainer.productList.get("an"));
}
break;
}
答案 3 :(得分:1)
如果使用else if替换
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"));
}
else if (inputCommand.readLine().equals("an")){
basketCommand.addIntoBasket(productContainer.productList.get("an"));
}
break;
答案 4 :(得分:1)
我认为这是一个用于学习的基本程序,所以我尝试使其保持简单。
1。别忘了关闭Scanner类。
2。始终将数字输入转换为小写(考虑“ q”与“ Q”的大小写)
3。每次执行“ scanner.nextLine()”时,程序将停止并等待来自源的输入(在这种情况下,System.in配置为从键盘获取输入)
4.System.out.println将在输入结束时在屏幕上显示“ \ n”,从而导致创建新行,您可以在字符串中使用此char来保存代码行。
完整代码:
import java.util.Scanner;
import java.io.IOException;
public class MyClass {
public static void ShowMenu()
{
System.out.println("\nInstruction:");
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("q -- Quit Program");
}
public static void ProductMenu()
{
System.out.println("Select product to add into your basket");
System.out.println();
System.out.println("ba -- Basketball");
System.out.println("fi -- Fiat 500");
System.out.println("ip -- Iphone");
System.out.println();
}
public static void Exit(Scanner sc) {
sc.close();
System.out.println("You've exited the program. goodbye!");
System.exit(1);
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String inputCommand = null;
while (true) {
ShowMenu();
switch (inputCommand = scanner.nextLine().toLowerCase()) {
case "1":
System.out.println("you entered 1");
break;
case "2":
ProductMenu();
inputCommand = scanner.nextLine(); //should block
if (inputCommand.equals("ba")){
System.out.println("Basketball added.");
}
if (inputCommand.equals("fi")){
System.out.println("Fiat 500 added.");
}
if (inputCommand.equals("ip")){
System.out.println("Iphone added.");
}
break;
case "q":
Exit(scanner);
break;
default:
System.out.println("Invalid Input");
break;
}
}
}
}