【问题标题】:How can I write several conditions in a switch case to have same behavior? [closed]如何在 switch case 中编写多个条件以具有相同的行为? [关闭]
【发布时间】:2013-11-14 17:30:10
【问题描述】:

switch (x)
{
case A:
case B:
case C:
.doSomething()
}
有没有办法可以将这 3 个案例放在一条线上?例如像这样
case A, B, C:?

【问题讨论】:

  • 你有什么问题?
  • 无论你写什么,都会如你所愿。
  • 是的:case A: case B: case C:相同行中的三个。
  • @Reimeus 没什么我只是好奇是否有可能。例如,当声明整数值时,你可以有int xint yint zetc。或者你可以有int x, y, z 我只是想知道这是否可能与案例有关。

标签: java switch-statement case


【解决方案1】:

例如,当声明整数值时,您可以使用 int x int y int zetc。或者你可以有 int x, y, z 我只是想知道这是否可能与案例有关。

这是一个完全不同的问题,可能取决于你在做什么。

你可以有像这样的嵌套开关

switch(x) {
      case 1:
          switch(y) {
               case 2:
                    switch(z) {
                        case 3:

或者你可以使用一个公式,只要你知道这样的可能值的范围

switch(x * 100 + y * 10 + z) {
    case 123: // x = 1, y = 2, z = 3

显然这假设 x、y、z 介于 [0..9] 之间

【讨论】:

    【解决方案2】:

    除了删除换行符并将cases 全部放在同一行之外,没有。

    您必须拥有三个case 关键字和三个:

    如果您想了解详细信息,请参阅section 14.11 in the JLS。特别是:

    SwitchLabel:
        case ConstantExpression :
        case EnumConstantName :
        default :
    

    语法中没有模式可以接受像case A,B,C: 这样的SwitchLabel

    但是,当多个案例做同样的事情时,按照您的示例构建案例是一种常见的做法:

    switch (value) {
    case 1:
    case 3:
    case 5:
        System.out.println("It's a positive odd number less than 7!"); 
        break;
    case 4:
    case 8:
        System.out.println("It's a multiple of 4 between 1 and 9!");
        break;
    default:
        System.out.println("It's just another boring number.");
        break;
    }
    

    Java 程序员在阅读此类代码时通常会对其有一个清晰的理解。将多个案例放在一行中(即没有换行符)不太常见,并且不会被典型的程序员一眼就能清楚地理解(他们可能会认为您不小心删除了换行符)。

    【讨论】:

      【解决方案3】:

      不是你建议的方式,但你可以这样做(来自documentation):

      class SwitchDemo2 {
          public static void main(String[] args) {
      
              int month = 2;
              int year = 2000;
              int numDays = 0;
      
              switch (month) {
                  case 1: case 3: case 5:
                  case 7: case 8: case 10:
                  case 12:
                      numDays = 31;
                      break;
                  case 4: case 6:
                  case 9: case 11:
                      numDays = 30;
                      break;
                  case 2:
                      if (((year % 4 == 0) && 
                           !(year % 100 == 0))
                           || (year % 400 == 0))
                          numDays = 29;
                      else
                          numDays = 28;
                      break;
                  default:
                      System.out.println("Invalid month.");
                      break;
              }
              System.out.println("Number of Days = "
                                 + numDays);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        • 2016-11-30
        • 2019-11-26
        • 2020-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多