Java switch 语句多例

2022-01-19 00:00:00 syntax switch-statement java

只是想弄清楚如何为 Java switch 语句使用多种情况.这是我正在尝试做的一个示例:

Just trying to figure out how to use many multiple cases for a Java switch statement. Here's an example of what I'm trying to do:

switch (variable)
{
    case 5..100:
        doSomething();
    break;
}

与必须做的:

switch (variable)
{
    case 5:
    case 6:
    etc.
    case 100:
        doSomething();
    break;
}

如果可能的话,有什么想法,或者有什么好的替代方案?

Any ideas if this possible, or what a good alternative is?

推荐答案

遗憾的是,这在 Java 中是不可能的.您将不得不求助于使用 if-else 语句.

Sadly, it's not possible in Java. You'll have to resort to using if-else statements.

相关文章