如何处理多个条件的情况?

Afr*_*Man 23 java boolean case switch-statement

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

|| && < >
Run Code Online (Sandbox Code Playgroud)

明白我的意思了吗?

就像是

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

可悲的是,似乎没有这样的方式.我不能在switch case中进行布尔检查.

有办法解决吗?

顺便说一句,如果我听起来很混乱,非常抱歉.我还不太清楚这种语言的所有名称:X
任何答案都赞赏

Boh*_*ian 50

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

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

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


dev*_*ang 6

你可以做 -

switch(c) {
    case 'W':
    case 'w': //your code which will satisfy both cases
              break;

    // ....
}
Run Code Online (Sandbox Code Playgroud)