switch case 语句中的布尔逻辑 - Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26574388/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:10:50  来源:igfitidea点击:

Boolean logic in switch case statement - Java

javaswitch-statement

提问by Hyman

 switch (i) {
     case ("+" || "/"):  
        setOperator("i");
        break;
 }

What is the best way to do this in Java?

在 Java 中执行此操作的最佳方法是什么?

回答by Alex K

Of course.

当然。

Just use

只需使用

if(i.equals("+") || i.equals("/")) {
    setOperator("i");
}

OR if you have to use a switch statement, you can do it this way:

或者,如果您必须使用 switch 语句,您可以这样做:

switch(i) {
    case "+":
    case "/":
        setOperator("i");
        break;
}

Basically, you can't really have multiple cases the way you had thought about it. It's not the same structure as an if statement, where you can do various logical operations. Java does notgo through and do an if statement for each of the cases.

基本上,你不可能像你想象的那样有多个案例。它与 if 语句的结构不同,您可以在其中执行各种逻辑操作。Java并没有经历做一个if语句为每一个案件。

Instead, each time you have case("foo"), Java sees this as something called a Case Label. It is the reason that we sometimes opt to use switch statements, even though they are very primitive and sometimes not very convenient. Because we have case labels, the computer only has to do one evaluation, and it can jump to correct place and execute the right code.

相反,每次您拥有 时case("foo"),Java 都将其视为称为案例标签的东西。这就是我们有时选择使用 switch 语句的原因,即使它们非常原始并且有时不太方便。因为我们有 case 标签,计算机只需要做一次评估,它就可以跳转到正确的地方并执行正确的代码。

Here is a quote from a website that may help you:

这是一个网站的引用,可以帮助您:

A switch statement, as it is most often used, has the form:

最常用的 switch 语句具有以下形式:

switch (expression) {
   case constant-1:
      statements-1
      break;
   case constant-2:
      statements-2
      break;
      .
      .   // (more cases)
      .
   case constant-N:
      statements-N
      break;
   default:  // optional default case
      statements-(N+1)
} // end of switch statement

This has exactly the same effect as the following multiway if statement, but the switch statement can be more efficient because the computer can evaluate one expression and jump directly to the correct case, whereas in the if statement, the computer must evaluate up to N expressions before it knows which set of statements to execute:

这与下面的多路 if 语句效果完全相同,但 switch 语句效率更高,因为计算机可以计算一个表达式并直接跳转到正确的 case,而在 if 语句中,计算机必须计算最多 N 个表达式在它知道要执行哪组语句之前:

if (expression == constant-1) { // but use .equals for String!!
    statements-2
} 
else if (expression == constant-2) { 
    statements-3
} 
else
    .
    .
    .
else if (expression == constant-N) { 
    statements-N
} 
else {
    statements-(N+1)
}

回答by Rustam

yes you can do as: Fall throughin swith case

是的,你可以这样做:Fall throughswith case

      switch (i) {
        case "+":
        case "/":
            setOperator(i);
            break;
      }

回答by Abhi

switch (i) {
    case ("+"):
    case ("/"):
        setOperator("i");
        break;
    }