多条件案件怎么办?

2022-01-19 00:00:00 boolean switch-statement case java

在我使用任何编程语言的 1 个月经验中,我假设 switch case 条件将接受括号中的任何内容作为布尔检查 thingamajig, IE这些:

In the 1 month experience I've had with any programming language, I've assumed that switch case conditions would accept anything in the parenthesis as a boolean checking thingamajig, ie these:

|| && < >

明白我的意思吗?

类似

char someChar = 'w';
switch (someChar) {
case ('W' ||'w'):
    System.out.println ("W or w");
}

可悲的是,这种方式似乎并不奏效.我不能在开关盒中进行布尔检查.

Sadly, doesn't seem to work that way. I can't have boolean checking in switch case.

有办法解决吗?

顺便说一句,如果我听起来令人困惑,非常抱歉.我还不太清楚这种语言中所有内容的名称:X
任何答案表示赞赏

By the way, terribly sorry if I'm sounding confusing. I don't quite know the names for everything in this language yet :X
Any answers appreciated

推荐答案

对于这样的情况,您可以实现 OR:

You can achieve an OR for cases like this:

switch (someChsr) {
case 'w':
case 'W':
    // some code for 'w' or 'W'
    break;
case 'x': // etc
}

案例就像一个goto",多个 goto 可以共享同一行来开始执行.

Cases are like a "goto" and multiple gotos can share the same line to start execution.

相关文章