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

前端 未结 7 1133
梦毁少年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条回答
  • 2021-02-09 16:50

    It can be done. The code needs to be slightly altered.

    public static void main(String[]arguments)
        {
            int x=1, age=55;
            switch(x)
            {
            case 1: if((age>=60)&&(age<200))
                System.out.println("Senior Citizen");
            case 2: if((age>=18)&&(age<60))
                System.out.println("Adult");
            case 3: if((age>=12)&&(age<18))
                System.out.println("Teenager");
                break;
            default :
                System.out.println("Child");
                break;
            }
    }
    
    0 讨论(0)
提交回复
热议问题