Java 如何在switch语句中使用大于或等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18510360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use greater than or equal in a switch statement
提问by Vlad Holubiev
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:
使用 switch 语句检查变量是否大于某个数字的最佳方法是什么?或者你建议使用if-else?我找到了这样一个例子:
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?
关于在 switch 语句中使用“大于或等于”,你能告诉新手什么?
采纳答案by Stewart
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;
}
回答by Oliver Matthews
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.
Java 仅支持直接值,而不支持case
语句中的范围,因此如果您必须使用开关,请先映射到选项,然后再打开它,如您提供的示例所示,这是您唯一的选择。然而,这太过分了——只需使用 if 语句。
回答by Guillaume
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?
我强烈推荐一个if(var1>var2){}else if (var1==var2) {} else {}
构造。在此处使用开关将隐藏意图。如果 abreak
被错误删除怎么办?
Switch is useful and clear for enumerated values, not for comparisons.
Switch 对枚举值有用且清晰,而不是用于比较。
回答by Narendra Pathai
First a suggestion: switch
as it states should only be used for switching and not for condition checking.
首先是一个建议:switch
因为它指出应该只用于切换而不是用于条件检查。
SwitchStatement:
switch ( Expression ) SwitchBlock
Expressions convertible to int or Enum are supported in the expression.
表达式中支持可转换为 int 或 Enum 的表达式。
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.
据说这些标签与 switch 语句相关联,case 标签中的常量表达式(第 15.28 节)或枚举常量(第 8.9.1 节)的值也是如此。
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.
1.6 或更低版本的 switch 语句中只允许常量表达式和枚举常量,还支持 java 7 字符串值。不支持逻辑表达式。
Alternatively you can do as given by @Stewart in his answer.
或者,您可以按照@Stewart 在他的回答中给出的方法进行操作。
回答by Sajal Dutta
If onevariable's value is used, use switch. If multiplevariables are in play, use if. In your stated problem, it should be if.
如果使用一个变量的值,请使用 switch。如果有多个变量在起作用,请使用 if。在您陈述的问题中,应该是if。
回答by talegna
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:
switch 语句用于在返回特定值时运行代码,if then else 允许您在一个语句中选择一系列值。如果您想查看多个范围,我建议您执行以下操作(尽管我个人更喜欢 Integer.signum 方法):
int i;
if (var1 > var2) {
i = 1;
}
else if (var1 == var2) {
i = 0;
}
else {
i = -1;
}
回答by treeno
Unfortunately you cannot do that in java. It's possible in CoffeeScript
or other languages.
不幸的是,你不能在 Java 中做到这一点。可以使用CoffeeScript
或其他语言。
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.
我建议通过在额外的方法中移动你的“做事”来使用if
--else
语句。通过这种方式,您可以将 if-else 保持在清晰可读的代码中。
回答by chrylis -cautiouslyoptimistic-
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.
你最好使用这些if
语句;该switch
方法不太清楚,在这种情况下,您的switch
方法客观上是错误的。的合约Comparable#compareTo
不需要返回-1
or 1
,只是返回的值int
是负数或正数。compareTo
return是完全合法的-42
,您的switch
语句会丢弃结果。