简体   繁体   English

如何在switch语句中使用大于或等于

[英]How to use greater than or equal in a switch statement

What is the best way to check if variable is bigger than some number using switch statement?使用 switch 语句检查变量是否大于某个数字的最佳方法是什么? Or you reccomend to use if-else?或者你建议使用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?关于在 switch 语句中使用“大于或等于”,你能告诉新手什么?

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;
}

I would strongly recommend a if(var1>var2){}else if (var1==var2) {} else {} construct.我强烈推荐if(var1>var2){}else if (var1==var2) {} else {}构造。 Using a switch here will hide the intent.在此处使用开关将隐藏意图。 And what if a break is removed by error?如果一个break被错误删除了怎么办?

Switch is useful and clear for enumerated values, not for comparisons. Switch 对枚举值有用且清晰,而不是用于比较。

First a suggestion: switch as it states should only be used for switching and not for condition checking.首先是一个建议: switch状态应该只用于切换而不是条件检查。

From JLS switch statements 来自 JLS 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. 1.6 或更低版本的 switch 语句中只允许常量表达式和枚举常量,还支持 java 7 字符串值。 No logical expressions are supported.不支持逻辑表达式。

Alternatively you can do as given by @Stewart in his answer.或者,您可以按照@Stewart 在他的回答中给出的方法进行操作。

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. Java 仅支持直接值,不支持case语句中的范围,因此如果您必须使用 switch,首先映射到选项,然后打开它,如您提供的示例所示,这是您唯一的选择。 However that is quite excessive - just use the if statements.然而,这太过分了——只需使用 if 语句。

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. switch 语句用于在返回特定值时运行代码,if then else 允许您在一个语句中选择一系列值。 I would recommend doing something like the following (though I personnally prefer the Integer.signum method) should you want to look at multiple ranges:如果您想查看多个范围,我建议您执行以下操作(尽管我个人更喜欢 Integer.signum 方法):

int i;

if (var1 > var2) {
  i = 1;
}
else if (var1 == var2) {
  i = 0;
}
else {
  i = -1;
}

You're better off with the if statements;最好使用if语句; the switch approach is much less clear, and in this case, your switch approach is objectively wrong . switch方法不太清楚,在这种情况下,你的switch方法客观上是错误的 The contract for Comparable#compareTo does not require returning -1 or 1 , just that the value of the returned int be negative or positive. Comparable#compareTo的契约不要求返回-11 ,只要求返回的int值是负数或正数。 It's entirely legitimate for compareTo to return -42 , and your switch statement would drop the result. compareTo返回-42是完全合法的,并且您的switch语句会丢弃结果。

If one variable's value is used, use switch.如果使用一个变量的值,请使用 switch。 If multiple variables are in play, use if.如果有多个变量在起作用,请使用 if。 In your stated problem, it should be if .在您陈述的问题中,应该是if

Unfortunately you cannot do that in java.不幸的是,你不能在 Java 中做到这一点。 It's possible in CoffeeScript or other languages.这在CoffeeScript或其他语言中是可能的。

I would recommend to use an if - else -statement by moving your "do stuff" in extra methods.我建议通过在额外的方法中移动你的“做事”来使用if - else - 语句。 In that way you can keep your if-else in a clearly readable code.通过这种方式,您可以将 if-else 保持在清晰可读的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM