【问题标题】:If-else working, switch not [duplicate]如果 - 其他工作,切换不[重复]
【发布时间】:2013-06-25 02:08:16
【问题描述】:

我正在制作一个应用程序,它有一个带有文本的图像网格,每个图像都会打开一个不同的活动。它工作正常,但仅出于设计目的,我想用switch statements 替换我的if-else statements(我认为我可以这样做)但是它不起作用。现在我在每个图像上设置标签的工作代码是:

if(position == 0)
        textView.setText(R.string.zero);
    else if(position == 1)
        textView.setText(R.string.one);
    else if(position == 2)
        textView.setText(R.string.two);
    else if(position == 3)
        textView.setText(R.string.three);
    else if(position == 4)
        textView.setText(R.string.four);
    else if(position == 5)
        textView.setText(R.string.five);
ect....

我想使用:

switch(position)
case 0:
   textView.setText(R.string.zero);    
case 1:
   textView.setText(R.string.one);
case 2:
   textView.setText(R.string.two);    
case 3:
   textView.setText(R.string.three);
case 4:
   textView.setText(R.string.four);    

但是当我这样做时,那个标签是我定义的最后一个标签(在我的例子中它是“四个”)。对于每个对象,我也有一个类似的代码,以使用 position 变量启动不同的 intent ,但是这样做相反,并且使每个意图都等于第一个意图。我的语法是错误的还是对我的情况不起作用?

【问题讨论】:

    标签: java android if-statement switch-statement


    【解决方案1】:

    您需要在case 中的每个语句之后添加break;,否则执行会向下流动(您想要的所有案例也将被调用),因此您将始终得到最后一个案例。

    switch(position) {
    case 0:
        textView.setText(R.string.zero); 
        break; 
    case 1:
        textView.setText(R.string.one);
        break; 
    case 2:
        textView.setText(R.string.two);   
        break;  
    case 3:
        textView.setText(R.string.three);
        break; 
    case 4:
        textView.setText(R.string.four); 
        break; 
    }
    

    这里是 official tutorial 解释何时使用和何时不使用 break;

    【讨论】:

      【解决方案2】:

      每个分支后需要break;

      switch (position) {
          case 0:
              textView.setText(R.string.zero);
              break; // <-- here
          // etc
      }
      

      当你不存在break 时,switch 的合法使用被称为失败;或者因为你returnthrow.:

      switch (someNumber) {
          case 0:
              return 0; 
              // no need for break here
          case 1:
              throw new IllegalArgumentException();
              // no need to break here
          case 2:
              System.out.println("Oh, I got two!");
              // fall through
          case 3:
              return 3;
          default:
              System.out.println("Meh")
              // No need to break: last possible branch
      }
      
      return -1;
      

      即使输入 2 也会返回 3。

      否则你需要break

      【讨论】:

      • 在默认值的末尾放置一个中断总是一个很好的模式:因为有人可能会在它下面添加另一个案例并意外失败。
      【解决方案3】:

      在每个 case 之后使用 break 语句应该可以解决问题。在最后一种情况之后,我也会使用默认语句。

      【讨论】:

        【解决方案4】:

        这就是解决方案。您需要使用break 来避免遍历每个案例:

        switch(position)
        case 0:
           textView.setText(R.string.zero);    
           break;
        case 1:
           textView.setText(R.string.one);
           break;
        case 2:
           textView.setText(R.string.two);  
           break;  
        case 3:
           textView.setText(R.string.three);
           break;
        case 4:
           textView.setText(R.string.four);    
           break;
        

        我建议阅读有关 switch statement 的 oracle 文档。

        【讨论】:

          【解决方案5】:

          eace case 操作后需要使用 break 语句。在 switch-case 语句中,如果您不使用 break 语句,那么该特定情况之后的所有情况也将被执行

          case 0:
             textView.setText(R.string.zero);    
             break;
          

          【讨论】:

            【解决方案6】:

            不要忘记在每个案例之后加上break;:就像这样:

            switch(position){
            case 0:
               textView.setText(R.string.zero);    
               break;
            case 1:
               textView.setText(R.string.one);
               break;
            case 2:
               textView.setText(R.string.two);    
               break;
            case 3:
               textView.setText(R.string.three);
               break;
            case 4:
               textView.setText(R.string.four);  
               break;
            }
            

            【讨论】:

              【解决方案7】:

              Switch-case 语句中,您需要在每个 case 之后放置 break;

              switch(position){
              case 0:
                 textView.setText(R.string.zero);    
                 break;
              case 1:
                textView.setText(R.string.one);
                break;
              case 2:
                textView.setText(R.string.two);    
                break;
              case 3:
                textView.setText(R.string.three);
                break;
              case 4:
                textView.setText(R.string.four);  
                break;
              default:
                  System.out.println("not available");
              }
              

              你还需要把 default: 放在最后,因为当所有情况都错误时,执行 default: action。

              switch-case语句中不要忘记break;和默认操作。

              【讨论】:

              • 在默认值的末尾放置一个中断总是一个很好的模式:因为有人可能会在它下面添加另一个案例并意外失败。
              【解决方案8】:

              每个 break 语句都会终止封闭的 switch 语句。控制流继续 switch 块之后的第一条语句。 break 语句是必要的,因为没有它们,switch 块中的语句就会失败:匹配 case 标签之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句。

              switch 比 if-else 语句快

              底线:默认是可选的(像 switch 中的 else 语句一样工作),Break 是强制性的。

              有趣的事实:即使您忘记放置 break 语句,您也不会看到任何编译错误。

              【讨论】:

                【解决方案9】:

                switch 在每种情况下都需要break。但是在您的情况下,可以通过定义如下所示的数组来简单得多。

                String values = {R.string.zero, R.string.one, R.string.two, ... };
                

                使用它来填充 textView :textView.setText(values[position]);

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-06-30
                  • 1970-01-01
                  相关资源
                  最近更新 更多