Java 中的变量值开关案例

发布于 2024-10-17 06:16:13 字数 71 浏览 8 评论 0原文

我希望进行一个切换,其中 5 个案例在功能上相同,但还会有其他独特的案例。有没有办法列出处理 5 个不同值的 case 值?谢谢

I'm looking to make a switch where 5 of the cases are functionally identical, but then there will be other unique cases. Is there a way to list a case value that handles 5 different values? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

葬シ愛 2024-10-24 06:16:13

您可以在开关中复合标签

switch (variable) {
  case 'a': case 'b' : case 'c' : case 'd' :
     do something;
     break;
  case 'e': case 'f' :
     do something else
     break;
  default:
     do something;
}

将开关视为跳转到标签(可能与跳转(中断)到末尾相结合)会有所帮助。这意味着开关

switch (variable) {
  case 'a': case 'b' : case 'c' : case 'd' :
     do something;
     // note that there's no break here.
  case 'e': case 'f' :
     do something else
     break;
  default:
     do something;
}

将为“a”、“b”、“c”和“d”“做某事”和“做其他事”;而它只会为“e”和“f”“做其他事情”。最后,如果不是上述任何一个,它就会遇到“做某事”的默认块。

You can compound the labels in the switch

switch (variable) {
  case 'a': case 'b' : case 'c' : case 'd' :
     do something;
     break;
  case 'e': case 'f' :
     do something else
     break;
  default:
     do something;
}

Thinking of a switch as a jump to a label (possibly coupled with a jump (the break) to the end) will help. That means the switch

switch (variable) {
  case 'a': case 'b' : case 'c' : case 'd' :
     do something;
     // note that there's no break here.
  case 'e': case 'f' :
     do something else
     break;
  default:
     do something;
}

will "do something" and "do something else" for 'a', 'b', 'c', and 'd'; while it will only "do something else" for 'e' and 'f'. Finally if it's not any of the above it hits the default block of "do something".

半步萧音过轻尘 2024-10-24 06:16:13
switch (value) {
case 1:
case 2:
case 3:
case 4:
    doSomethingIdentical();
    break;
case 5:
    doSomethingDifferent();
    break;
default:
    break;
}
switch (value) {
case 1:
case 2:
case 3:
case 4:
    doSomethingIdentical();
    break;
case 5:
    doSomethingDifferent();
    break;
default:
    break;
}
べ映画 2024-10-24 06:16:13

这很容易做到。不要只使用一个 case 值来处理所有 5 个不同的值,而是让 5 个 case 值相互传递,如下所示:


switch(value)
{
    case 1:
    case 2:
        //case 1 and 2 will both result in this code being executed
        doSomething();
        break;
    case 3:
        doSomethingElse();
        break;
}

This is very easy to do. Instead of just having one case value that handles all 5 different values, let the 5 case values fall through to each other, like so:


switch(value)
{
    case 1:
    case 2:
        //case 1 and 2 will both result in this code being executed
        doSomething();
        break;
    case 3:
        doSomethingElse();
        break;
}
暮年 2024-10-24 06:16:13

只要您不在开关上放置 break; ,它就会跳到下一条语句。

这样,你就可以得到这样的东西:

String value(int val) {
    String out = "";
    switch(val) {
    case 0:
        out = "case 0";
        break;
    case 1:
        out = "case 1";
        break;
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        out = "case 2, 3, 4, 5 or 6";
        break;
    case 7:
        out = "case 7";
        break;
    }
    return out;
}

As long as you don't put a break; on a switch it will fall through to the next statement.

In that way, you can have something like this:

String value(int val) {
    String out = "";
    switch(val) {
    case 0:
        out = "case 0";
        break;
    case 1:
        out = "case 1";
        break;
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
        out = "case 2, 3, 4, 5 or 6";
        break;
    case 7:
        out = "case 7";
        break;
    }
    return out;
}
ζ澈沫 2024-10-24 06:16:13

是的,只需使用这样的开关:

switch(v) {

   case 1:
   case 2:
   case 3:
       identicFunctionality();
       break;
   case 4:
       other01();
       break;
   case 5:
       other02();
       break;
   default:
       _default();
}

Yes, just use a switch like this:

switch(v) {

   case 1:
   case 2:
   case 3:
       identicFunctionality();
       break;
   case 4:
       other01();
       break;
   case 5:
       other02();
       break;
   default:
       _default();
}
苏大泽ㄣ 2024-10-24 06:16:13

从 Java 12 开始,我相信它是受支持的。查看 JEP 354。我从未使用过该语法,但我认为对于您的情况,它会像这样运行。

switch (day) {
    case 1, 2, 3, 4, 5    -> System.out.println("1-5");
    case 7                -> System.out.println(7);
    case 8                -> System.out.println(8);
    case 9                -> System.out.println(9);
}

相关说明 JEP 325 也很酷。

As of Java 12 I believe it is supported. Check out JEP 354. I have never used the syntax but I think it would run like this for your case.

switch (day) {
    case 1, 2, 3, 4, 5    -> System.out.println("1-5");
    case 7                -> System.out.println(7);
    case 8                -> System.out.println(8);
    case 9                -> System.out.println(9);
}

on a related note JEP 325 is also cool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文