Java switch statement multiple cases

前端 未结 13 938
后悔当初
后悔当初 2020-11-27 14:43

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)
{
    c         


        
相关标签:
13条回答
  • 2020-11-27 15:12

    From the last java-12 release multiple constants in the same case label is available in preview language feature

    It is available in a JDK feature release to provoke developer feedback based on real world use; this may lead to it becoming permanent in a future Java SE Platform.

    It looks like:

    switch(variable) {
        case 1 -> doSomething();
        case 2, 3, 4 -> doSomethingElse();
    };
    

    See more JEP 325: Switch Expressions (Preview)

    0 讨论(0)
提交回复
热议问题