How to use greater than or equal in a switch statement

后端 未结 8 2369
忘掉有多难
忘掉有多难 2021-02-19 04:10

What is the best way to check if variable is bigger than some number using switch statement? Or you reccomend to use if-else? I found such an example:

int i;

i         


        
8条回答
  •  旧时难觅i
    2021-02-19 04:45

    You're better off with the if statements; the switch approach is much less clear, and in this case, your switch approach is objectively wrong. The contract for Comparable#compareTo does not require returning -1 or 1, just that the value of the returned int be negative or positive. It's entirely legitimate for compareTo to return -42, and your switch statement would drop the result.

提交回复
热议问题