14
4

9 回答 9

26

我建议准确定义应该执行哪些语句:

switch (variable){
    case A: 
        statement_1();
        statement_3();
        break;
    case B: 
        statement_2();
        statement_3();
        break;
}

对于更新 3:

为这 10 行创建方法:

public void statement_1() {
    //your 10 lines of code
}

如果您总是在执行 statement_3,除了 case C,您可以在编写时使用 if/else-blocks。

但老实说:如果您有少量案例,请准确定义在这种情况下必须做什么。别人更容易阅读

于 2013-07-23T15:48:51.133 回答
3

你可以这样做:

switch (variable){
  case A:  do statement_1; do statement_3; break;
  case B:  do statement_2; do statement_3; break;    
}
于 2013-07-23T15:49:51.413 回答
1

为什么不将 switch 嵌套到 if 语句中?这种方式没有重复代码。

if(!C){
    statement_3;
    switch(variable){
    case A:
        statement_1;
        break;
    case B: 
        statement_2;
        break;
}

还是同时使用 if 语句和 switch?

if(!C){
    statement_3;
}
switch(variable){
case A:
    statement_1;
    break;
case B: 
    statement_2;
    break;
于 2013-07-23T16:08:15.317 回答
0

我经常发现引入enums 可以增加清晰度。在这里,我想每个enum都是可以通过许多过程解决的问题:

enum Issue {
  A {
    void handleIt () {
      statement_1();
      statement_3();
    }
  },
  B {
    void handleIt () {
      statement_2();
      statement_3();
    }
  },
  C {
    void handleIt () {
      // Do nothing.
    }
  },
  D {
    void handleIt () {
      A.handleIt();
      B.handleIt();
    }
  };

  abstract void handleIt();
}

请注意,您可以获得额外的好处,即能够使用其他问题的解决方案来处理某些问题(请参阅我的D enum)。

于 2013-07-23T16:13:27.003 回答
0

如果一个案例有超过 2-3 个语句,最好(从可读性和干净代码的角度)将它们提取为单独的方法:

switch (variable){
    case A: handleCaseA(); break;
    case B: handleCaseB(); break;
    default: handleDefaultCase(); break;
}
于 2013-07-23T16:17:27.380 回答
0
switch (variable) {
case A:  
do statement_1;
do statement_3;
break;
case B:
do statement_2;
do statement_3;
break;
}
于 2016-05-25T11:06:49.270 回答
0

使用 Java 12 switch-expressions你可以很好地做到这一点,如下所示:

switch (variable) {
        case A -> statement_1();
                  statement_3();
        case B -> statement_2();
                  statement_3();
};
于 2021-03-16T08:20:17.783 回答
-1

当您使用“case”时,首先使用 switch 语句,并确保其中有一个已初始化的变量。这是一个例子。

使用初始化变量:即

(String s=sc.nextLine();)

        switch (s) {
        case "Pepperoni":
            System.out.println("Pepperoni? Nice choice. Please wait............Ding! Here's your pepperoni pizza. Enjoy, and thanks for using the Local Pizzeria!");
        case "Cheese":
            System.out.println("Cheese? Okay! Soft cheese it is! Please wait............Ding! Here's your cheese pizza. Enjoy, and thank you for using the Local Pizzeria!");
            break;
        case "Mushrooms":
            System.out.println("Mushrooms? Veggie person, right? OK! Please wait............Ding! Here's your mushroom pizza. Enjoy, and thanks for using the Local Pizzeria!");
            break;
        case "Beef Bits":
            System.out.println("Beef bits? Like meat? Alright! Please wait............Ding! Here's your beef bit pizza. Enjoy, and thank you for using the Local Pizzeria!");

            break;
        case "Spicy Buffalo Chicken":
            System.out.println("Spicy buffalo chicken? Way to spice things up! Please wait............Ding! Here's your spicy buffalo chicken pizza. Enjoy, and thank you for using the Local Pizzeria!");
            break;
        case "Exit":
            System.out.println("See you soon!");
        }
    }
于 2020-01-15T03:35:29.590 回答
-2

在这里你必须if statement以这种方式使用,因为switch通常default也有值。

    if (letter == 'A' || letter == 'B') { System.out.println("Statement 3 ");}
switch (letter) {
            case 'A':
                System.out.println("Statement 1 ");
                break;

            case 'B':
                System.out.println("Statement 2 ");
                break;

            case 'C':
                System.out.println();
                break;
            default:
                System.out.println("You entered wrong value");
}       
于 2018-02-26T13:29:11.827 回答