【问题标题】:Java switch case fall throughJava switch 案例失败
【发布时间】:2013-12-08 04:19:24
【问题描述】:

我在 Java 中有一个这样的 switch case:

switch(int example)
{
   case 1: //Do different
      break;
   case 2: //Do different 
      break;
   /** For int more than 2, then I need
       for it to do something same.
   */
   case 3://Do different and case6
      break;
   case 4://Do different and case6
      break;
   case 5://Do different and case6
      break;
   case 6:
      break;
}

什么是一种优雅的方式来做到这一点,没有一个案例 3-5 调用的特殊案例 6 函数? (我这里用的是int,不过那只是个例子,所以不能用if(int >2)

【问题讨论】:

    标签: java switch-statement


    【解决方案1】:

    我能想到的一种方法是将您的代码移动到不同的函数中。像这样。

    void case1(){
        // do something
    }
    ...
    void case3(){
       // do something
        case6();
    }
    ...
    void case6(){
       // do something
    }
    
    // This switch is in some other method.
    switch(int example)
    {
       case 1: //Do different
          case1();
          break;
       ...
       case 3://Do different and case6
          case3(); //internally calls case 6
          break;
       ...
       case 6:
          case6();
          break;
    }
    

    或者您甚至可以为每种情况使用不同的方法,并在case 3: 中调用case3()case6() 方法。无论哪种方式,方法解决方案都可以工作,恕我直言,它会更加优雅和多个switch 语句。

    【讨论】:

      【解决方案2】:

      我不确定它是否优雅,但一种方法是拥有两个 switch 块:

      switch(int example)
      {
         case 1: //Do different
            break;
         case 2: //Do different 
            break;
         case 3:
            // Do whatever
            break;
         case 4:
            // Do whatever
            break;
         case 5:
            // Do whatever
            break;
      }
      
      switch(int example)
      {
         case 3:
         case 4:
         case 5:
         case 6:
            // Do whatever (case 3-5 fall through)
            break;
      }
      

      【讨论】:

      • case 3 不应运行 case 4 或 5,而应仅运行 case 6。不过谢谢。
      • @Secret 在第二个switch 语句中,您在案例 3 到案例 5 中没有做任何事情。他们只是让它落到案例 6 中。
      • 哦,是的,我在那里有点失明。 =) Radiodef 的回答我觉得更紧凑,但这是最安全的,因为我们不运行默认值。
      【解决方案3】:

      开箱即用的开关无法真正做到您所要求的。不过,您可以使用嵌套开关构造类似的东西:

      outer_switch: switch (example) {
      
          case 1: System.out.println("1");
                  break;
      
          case 2: System.out.println("2");
                  break;
      
          default: {
              switch (example) {
      
                  case 3: System.out.println("3");
                          break;
      
                  case 4: System.out.println("4");
                          break;
      
                  case 5: System.out.println("5");
                          break;
      
                  case 6: System.out.println("6");
                          break;
      
                  default: break outer_switch;
              }
      
              System.out.println("not 1 nor 2");
          }
      }
      

      注意outer_switch 上标记的中断,如果example 不满足任何内部情况,这是一种规避共享代码的方法。

      【讨论】:

        【解决方案4】:

        虽然您的代码并不漂亮,但它可能会给您带来不错的性能。您的另一个明显选择是 if-elseif-else 语句。请参阅已接受的答案 here 了解为什么 switch 可能是最佳选择,并参阅here 了解在 Java 中使用大型 switch 语句可能会遇到哪些性能问题。

        【讨论】:

          【解决方案5】:

          这也可能是您想要实现的解决方案:

                 switch(example){
                      case 1:
                          System.out.println(example);
                          break;
                      case 2:
                          System.out.println(example);
                          break;
                      case 3:
                          System.out.println("I'm case 3");
                      case 4:
                          if (example == 4){
                              System.out.println("I'm case 4");
                          }
                      case 5:
                          if (example == 5){
                              System.out.println("I'm case 5");
                          }
                      case 6:
                          System.out.println("I'm in extra case " + example);
                          break;
                  }
          

          这个想法是您添加一个额外的条件检查,让您的代码通过所有分支而不执行不必要的分支。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-12-19
            • 1970-01-01
            • 2012-08-14
            • 1970-01-01
            • 2013-11-23
            • 1970-01-01
            • 2010-10-12
            相关资源
            最近更新 更多