问题
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;
if(var1>var2) i = 1;
if(var1=var2 i = 0;
if(var1<var2) i = -1;
switch (i);
{
case -1:
do stuff;
break;
case 0:
do stuff;
break;
case 1:
do stuff;
break;
}
What can you tell a novice about using "greater than or equal" in switch statements?
回答1:
Not sure if this is what you're asking, but you could do it this way:
int var1;
int var2;
int signum = Long.signum((long)var1 - var2);
switch(signum) {
case -1: break;
case 0: break;
case 1: break;
}
回答2:
I would strongly recommend a if(var1>var2){}else if (var1==var2) {} else {}
construct. Using a switch here will hide the intent. And what if a break
is removed by error?
Switch is useful and clear for enumerated values, not for comparisons.
回答3:
First a suggestion: switch
as it states should only be used for switching and not for condition checking.
From JLS switch statements
SwitchStatement:
switch ( Expression ) SwitchBlock
Expressions convertible to int or Enum are supported in the expression.
These labels are said to be associated with the switch statement, as are the values of the constant expressions (§15.28) or enum constants (§8.9.1) in the case labels.
Only constant expressions and Enum constants are allowed in switch statements for 1.6 or lower with java 7 String values are also supported. No logical expressions are supported.
Alternatively you can do as given by @Stewart in his answer.
回答4:
Java only supports direct values, not ranges in case
statements, so if you must use a switch, mapping to options first, then switching on that, as in the example you provide is your only choice. However that is quite excessive - just use the if statements.
回答5:
A switch statement is for running code when specific values are returned, the if then else allows you to select a range of values in one statement. I would recommend doing something like the following (though I personnally prefer the Integer.signum method) should you want to look at multiple ranges:
int i;
if (var1 > var2) {
i = 1;
}
else if (var1 == var2) {
i = 0;
}
else {
i = -1;
}
回答6:
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.
回答7:
If one variable's value is used, use switch. If multiple variables are in play, use if. In your stated problem, it should be if.
回答8:
Unfortunately you cannot do that in java. It's possible in CoffeeScript
or other languages.
I would recommend to use an if
-else
-statement by moving your "do stuff" in extra methods. In that way you can keep your if-else in a clearly readable code.
来源:https://stackoverflow.com/questions/18510360/how-to-use-greater-than-or-equal-in-a-switch-statement