use relational operators in switch

2019-01-19 22:20发布

Is there a way to use relational operators (<,<=,>,>=) in a switch statement?

int score = 95;

switch(score)  {
   case (score >= 90):
      // do stuff
}

the above example (obviously) doesn't work

7条回答
祖国的老花朵
2楼-- · 2019-01-19 23:07

It will never work. You should understand what switch does in the first place.

It will execute the statements falling under the case which matches the switch argument.

In this case, score is an argument which is 95 but score>=90 will always evaluate to either true or false and never matches an integer.

You should use if statements instead.

Also Java doesn't allow booleans in switch cases so yea.

查看更多
看我几分像从前
3楼-- · 2019-01-19 23:08

Unfortunately NO, though you can use case fall (kind of hacky) by grouping multiple case statements without break and implement code when a range ends:

int score = 95;
switch(score) {
 ..
 case 79: System.out.println("value in 70-79 range"); break;
 case 80:
 ..
 case 85: System.out.println("value in 80-85 range"); break;
 case 90:
 case 91:
 case 92:
 case 93:
 case 94:
 case 95: System.out.println("value in 90-95 range"); break;
 default: break;
}

IMHO, using if would be more appropriate in your particular case.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-19 23:10

Simply NO

int score = 95;

switch(score)  {
   case (score >= 90):
      // do stuff
}

You are passing a int value to switch. So the case's must be in int values, where

(score >= 90)

Turns boolean.

Your case is a good candidaate for if else

查看更多
该账号已被封号
5楼-- · 2019-01-19 23:11

The docs for switch-case statement state:

a switch statement tests expressions based only on a single integer, enumerated value, or String object.

So there is no boolean. Doing so would make no sence since you only have two values: true or false.

What you could do is write a method which checks the score and then returns a one of the types switch can handle

For example:

enum CheckScore {
    SCORE_HIGHER_EQUAL_90,
    ...
}


public CheckScore checkScore(int score) {
    if(score >= 90) {
        return SCORE_HIGHER_EQUAL_90;
    } else if(...) {
        return ...
    }
}

and then use it in your switch:

switch(checkScore(score))  {
   case SCORE_HIGHER_EQUAL_90:
      // do stuff
}

... Or You could just use if, else-if, else directly!

查看更多
看我几分像从前
6楼-- · 2019-01-19 23:16

This might help you if you need to do it with switch itself,

char g ='X';
            int marks = 65;
            switch(marks/10)
            {
                case 1:
                case 2:
                case 3:
                case 4: g = 'F';
                        break;
                case 5: g = 'E';
                        break;
                case 6: g = 'D';
                        break;
                case 7: g = 'C';
                        break;
                case 8: g = 'B';
                        break;
                case 9: 
                case 10: g = 'A';       
                         break;
            }
            System.out.println(g);

It works this way,

    if(marks<50)
                g='F';
            else if(marks<60)
                g='E';
            else if(marks<70)
                g='D';
            else if(marks<80)
                g='C';
            else if(marks<90)
                g='B';
            else if(marks<=100)
                g='A';
查看更多
冷血范
7楼-- · 2019-01-19 23:20

No you can not.
From jls-14.11

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.  

Relational operators (<,<=,>,>=) results in boolean and which is not allowded.

All of the following must be true, or a compile-time error occurs:

  • Every case constant expression associated with a switch statement must be assignable (§5.2) to the type of the switch Expression.

  • No two of the case constant expressions associated with a switch statement may have the same value.

  • No switch label is null.

  • At most one default label may be associated with the same switch statement.

查看更多
登录 后发表回答