【问题标题】:A few if in a switch-case block一些 if 在 switch-case 块中
【发布时间】: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


【解决方案1】:

在第二个case语句中,你应该只读取下一个输入:

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 next = inputCommand.readLine();
        if (next.equals("su")) {
            basketCommand.addIntoBasket(productContainer.productList.get("su"));
        }
        else if (next.equals("an")) {
            basketCommand.addIntoBasket(productContainer.productList.get("an"));
        }
        break;
    }
}

【讨论】:

  • 正确的你在这里+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"));
                }

【讨论】:

    【解决方案3】:

    每次您执行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;
    }
    

    【讨论】:

      【解决方案4】:

      将 if 替换为 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;
      

      【讨论】:

      • 正确的解决方案,错误的问题。该错误是ifs 都在请求输入,而他们应该只是测试在第一个if 之前完成的输入的结果。
      【解决方案5】:

      我认为这是一个用于学习目的的基本程序,所以我尽量保持简单。

      1.不要忘记关闭 Scanner 类。

      2.始终将数字输入转换为小写(考虑“q”与“Q”的大小写)

      3.每次你执行“scanner.nextLine()”你的程序将停止并等待来自源的输入(在这种情况下 System.in 配置为从键盘输入)

      4.System.out.println 将在其输入结束时将“\n”打印到屏幕上,从而创建一个新行,您可以在字符串中使用此字符来保存代码行。

      完整代码:

      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;
              }
         }
      
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 1970-01-01
        相关资源
        最近更新 更多