Can you use conditional statements in switch-case in Android?

前端 未结 7 1131
梦毁少年i
梦毁少年i 2021-02-09 16:07

I can\'t seem to find a straight forward yes or no to this in my searching. In Android, is there a way to use a conditional statement in case-switch? For example, with age being

7条回答
  •  -上瘾入骨i
    2021-02-09 16:45

    No. You cannot do this,

    switch (age){
    case (>79):
      // Do this stuff
      break;
    case (>50):
      // Do this other stuff
      break;
    }
    

    You need an if and else,

    if (age > 79) {
     // do this stuff.
    } else if (age > 50) {
     // do this other stuff.
    } // ...
    

提交回复
热议问题