use relational operators in switch

前端 未结 7 2069
终归单人心
终归单人心 2020-12-11 06:55

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

int score = 95;

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


        
相关标签:
7条回答
  • 2020-12-11 07:48

    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

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