JavaScript 切换逻辑运算符?

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

JavaScript switch with logical operators?

javascriptswitch-statement

提问by Strawberry

for (var count = 1; count < 6; count++) {
    switch (count) {
        case (2):   document.write("hi"); break;     
        case (count > 3):   document.write("bye"); break;    
        case (count >= 4): document.write("lol"); break;
    }
}

Because it's not working the way I expect, not printing bye and lol, it makes me think this is invalid in JavaScript. I tried looking for some examples to see if people do this on Google, and I saw none. So is this valid or not? or Why might this not work?

因为它不像我期望的那样工作,不打印再见和大声笑,这让我觉得这在 JavaScript 中是无效的。我试着找一些例子来看看人们是否在谷歌上这样做,但我没有看到。那么这是有效的还是无效的?或者为什么这可能不起作用?

回答by Gumbo

When switchis interpreted, the expression in the parentheses is compared to values of the particular cases.

switch被解释,在括号中的表达比的特定情况下的值。

So in your case the value of countwould be compared to the values of 2, count > 3and count >= 4. And that won't work. Although you can rewrite it and compare to trueto get it working:

因此,在您的情况下, 的值count将与2,count > 3和的值进行比较count >= 4。那是行不通的。尽管您可以重写它并进行比较以true使其正常工作:

switch (true) {
    case (count == 2):
        document.write("hi");
        break;
    case (count > 3):
        document.write("bye");
        break;
    case (count >= 4):
        document.write("lol");
        break;
}

But that's not how switchis supposed to be used.

但这不是switch应该如何使用。

Use ifstatements instead:

改用if语句:

if (count == 2) {
    document.write("hi");
} else if (count > 3) {
    document.write("bye");
} else if (count >= 4) {
    document.write("lol");
}


Edit????Since you use the switchcases exclusively (break if a case matches), my switch-to-if/elsetranslation is correct.

编辑????由于您使用的switch情况下,专门(如果休息的情况下比赛),我switch-到- if/else翻译是正确的。

But the count >= 4case/branch will never be applied since count > 3is true (also) for countvalues greater or equal 4.

但是count >= 4case/branch 永远不会被应用,因为count > 3对于count大于或等于 4 的值也是如此。

To fix this problem (write “bye” and“lol” for values greater or equal 4), remove the last elseto make the last ifstatement independent from the preceding:

要解决此问题(将“bye”“lol”写入大于或等于 4 的值),删除最后else一个if语句以使最后一个语句独立于前面的语句:

if (count == 2) {
    document.write("hi");
} else if (count > 3) {
    document.write("bye");
}
if (count >= 4) {
    document.write("lol");
}

回答by Dan Breslau

This is a correction to Gumbo's answer. I'm writing a separate answer only because this won't fit as a comment.

这是对Gumbo 答案的更正。我写一个单独的答案只是因为这不适合作为评论。

Edit:Gumbo suggested in a comment that I may have misread Doug's intention. If the OP really wants both "bye" and "lol" to be printed out for count >= 4, then we need to remove a breakfrom the switch. The cases are now back in the original order, so that "bye" and "lol" are printed in that order (which is apparently the OP's intent.)

编辑:Gumbo 在评论中建议我可能误读了 Doug 的意图。如果 OP 真的希望打印出“bye”和“lol”以计数 >= 4,那么我们需要breakswitch. 这些案例现在恢复到原来的顺序,因此“再见”和“大声笑”按该顺序打印(这显然是 OP 的意图。)

switch (true) {
    case (count == 2):
        document.write("hi");
        break;
    case (count > 3):
        document.write("bye");
        // No break here; just fall through.
    case (count >= 4):
        document.write("lol");
        break;
}

In this case, I agree with Gumbo that the revised ifstatement is correct.

在这种情况下,我同意 Gumbo 的意见,即修改后的if声明是正确的。

Original answer follows (assumes that the OP really wanted either"lol" or "bye" to print, but not both.)

原来的答案如下(假设OP真的想无论是“笑”或“再见”来打印,但不能同时使用。)

The switchstatement that Gumbo wrotewon't work for count >= 4, for much the same reason that Gumbo's original ifstatement won't work: Because the cases are evaluated in sequence, count >= 4 implies that the second case (count > 3) will be executed; so the script will never reach the test for count >= 4. To fix this, the tests should be executed in the reverse order, from highest to lowest:

Gumbo 编写switch语句不适用于 count >= 4,原因与 Gumbo 的原始语句不起作用的原因大致相同:因为案例是按顺序计算的,count >= 4 意味着第二种情况 (count > 3 ) 将被执行;所以脚本永远不会到达 count >= 4 的测试。为了解决这个问题,测试应该以相反的顺序执行,从高到低:if

switch (true) {
    case (count >= 4):
        document.write("lol");
        break;
    case (count > 3):
        document.write("bye");
        break;
    case (count == 2):
        document.write("hi");
        break;
}

The corrected ifstatement is still not right either, because for count >= 4 it will produce bothbyeand lolon the output. Again, the tests within the ifladder should be arranged to go from highest to lowest values:

更正后的if语句仍然不正确,因为对于 count >= 4 它将在输出中同时产生byelol。同样,if阶梯内的测试应安排为从最高值到最低值:

if (count >= 4) {
    document.write("lol");
} else if (count > 3) {
    document.write("bye"); 
} else if (count == 2) {
    document.write("hi");
}

This isn't an ideal example, because if countis an integer, then evaluating count >= 4and count > 3will produce the same results -- truefor count >= 4, falseotherwise. That wouldn't be the case if countis a floating-point value (but then, a floating-point value named "count" would raise other concerns.)

这不是一个理想的例子,因为如果count是一个整数,那么计算count >= 4count > 3将产生相同的结果——true计数> = 4,false否则。如果count是浮点值,则情况并非如此(但是,名为“count”的浮点值会引起其他问题。)

回答by anthares

You use the case clause in the wrong way. You should provide a value that will be compared to the value in the switch clause ... and not a boolean expression like this count>2

您以错误的方式使用 case 子句。您应该提供一个将与 switch 子句中的值进行比较的值......而不是像这样的布尔表达式count>2

In this case this boolean expression will be cast to true or false (1 or 0) and compared to your value count and sometimes may work, sometimes - not.

在这种情况下,这个布尔表达式将被转换为 true 或 false(1 或 0)并与您的值计数进行比较,有时可能会起作用,有时 - 不会。

You should consider replacing it with if statements.

您应该考虑用 if 语句替换它。

回答by Sarfraz

The switchnormally needs a fixedcondition/value; because your countvariable changes every time, it goes against that. Use if-elsecondition instead.

switch通常需要一个固定的状态/值; 因为您的count变量每次都在变化,所以与此相反。改用if-else条件。

回答by brian

You should swap your last two cases.

你应该交换你的最后两个案例。