Switch statement just returning the last case

前端 未结 4 819
[愿得一人]
[愿得一人] 2021-01-24 00:52

Switch statment fix: The switch statement is only returning the last case i.e case 4, \"#0R0dfdf0FF\". how can i fix this so the text view shows the the one cli

相关标签:
4条回答
  • 2021-01-24 01:26

    You need a break when you don't have a return otherwise it causes fall through

    0 讨论(0)
  • 2021-01-24 01:35

    You need the break; after all cases except for the last one or else it'll fall through case by case

     switch (which){
            case 0:
                mColor.setText("#000000");
                break;        
            case 1:
                mColor.setText("#0000FF");
                break;        
            case 2:
                mColor.setText("#0R00FF");
                break; 
            case 3:
                mColor.setText("#0R00dsdFF");
                break; 
            case 4:
                mColor.setText("#0R0dfdf0FF");
            default:  
                break; 
            }
    
    0 讨论(0)
  • 2021-01-24 01:37

    You are missing break; at the end of the switch branches.

    0 讨论(0)
  • 2021-01-24 01:37

    Fall Through.

    You have to add the break.

    case 0:
              mColor.setText("#000000");
              break;
    

    You can find that in docs

    The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

    0 讨论(0)
提交回复
热议问题