开关多次调用同一个案例 我想用java编写一些代码,可以在交换机中或某些情况下调用相同的情况多次,使用一些代码,对于大多数或所有中间的情况都是相同的。现在我必须为每个案例重复一半的代码,因为它周围都是区分大小写的代码。我心目中的代码是这样的,变量范围为0-3,break表示在下一次调用之前停止执行,我知道它可能是break之外的东西,如果它存在的话 switch(variable){ case 0: case 1: if(other factors) //add item to next spot in array case 2: case 3://all cases //add items to next 3 spots in array for all cases break; case 0: case 1: if(other factors) //add item to next spot in array case 2: case 3://all cases //add more items to next spot in array break; case 1: case 2: if(other factors2) //add item to next spot in array break; case 3: //add item to next spot in array case 0: case 1: case 2://all cases //add items to next spot in array break; case 1: case 2: if(other factors2) //add item to next spot in array break; case 3: //add item to next spot in array }

开关多次调用同一个案例 我想用java编写一些代码,可以在交换机中或某些情况下调用相同的情况多次,使用一些代码,对于大多数或所有中间的情况都是相同的。现在我必须为每个案例重复一半的代码,因为它周围都是区分大小写的代码。我心目中的代码是这样的,变量范围为0-3,break表示在下一次调用之前停止执行,我知道它可能是break之外的东西,如果它存在的话 switch(variable){ case 0: case 1: if(other factors) //add item to next spot in array case 2: case 3://all cases //add items to next 3 spots in array for all cases break; case 0: case 1: if(other factors) //add item to next spot in array case 2: case 3://all cases //add more items to next spot in array break; case 1: case 2: if(other factors2) //add item to next spot in array break; case 3: //add item to next spot in array case 0: case 1: case 2://all cases //add items to next spot in array break; case 1: case 2: if(other factors2) //add item to next spot in array break; case 3: //add item to next spot in array },java,switch-statement,Java,Switch Statement,您可以使用多个switch语句来实现这一点。使用break时它在任何情况下都会退出开关块。您可以使用多个开关语句来完成此操作。使用break时在任何情况下,它都会退出开关块。开关不适用于此,您需要使用一些if-else语句(或Peter说的一些单独的开关语句)执行检查 发件人: 与switch语句关联的两个case常量表达式不能具有相同的值 Switch不适合这种情况,您需要使用一些if-else语句(或Peter所说的一些单独的Switch语句)执行检查 发件人: 与switch语句关联的两个

您可以使用多个switch语句来实现这一点。使用
break时它在任何情况下都会退出开关块。

您可以使用多个开关语句来完成此操作。使用
break时在任何情况下,它都会退出开关块。

开关不适用于此,您需要使用一些
if-else
语句(或Peter说的一些单独的开关语句)执行检查

发件人:

与switch语句关联的两个case常量表达式不能具有相同的值


Switch不适合这种情况,您需要使用一些
if-else
语句(或Peter所说的一些单独的Switch语句)执行检查

发件人:

与switch语句关联的两个case常量表达式不能具有相同的值


首先,我要将伪开关拆分为实开关:

switch(variable){
case 0:
case 1:
    if(other factors)
        //add item to next spot in array
case 2:
case 3://all cases
    //add items to next 3 spots in array for all cases
}

switch(variable){
case 0:
case 1:
    if(other factors)
        //add item to next spot in array
case 2:
case 3://all cases
    //add more items to next spot in array
}

switch(variable){
case 1:
case 2:
    if(other factors2)
        //add item to next spot in array
    break;
case 3:
    //add item to next spot in array
case 0:

}

switch(variable){
case 1:
case 2://all cases
    //add items to next spot in array
    break;
case 1:
case 2:
    if(other factors2)
        //add item to next spot in array
}
这应该符合你的要求

然后,我将用自己的方法提取每个开关块,以使其更易于理解和阅读

你可以考虑把所有这些都提取到一个小类HIRACHY中:

class DefaultExecutor{
    void do(){
        step1();
        step2();
        step3();
        step4();
    }
    void step1(){//all cases class of the first switch statement}
    //... similar methods for the other switcht statements
}

class CaseZeor extends DefaultExecutor{
    // override step1-4 as required for special treatment of case 0
}

// ... further classes for cases 1-3

首先,我要将伪开关拆分为实开关:

switch(variable){
case 0:
case 1:
    if(other factors)
        //add item to next spot in array
case 2:
case 3://all cases
    //add items to next 3 spots in array for all cases
}

switch(variable){
case 0:
case 1:
    if(other factors)
        //add item to next spot in array
case 2:
case 3://all cases
    //add more items to next spot in array
}

switch(variable){
case 1:
case 2:
    if(other factors2)
        //add item to next spot in array
    break;
case 3:
    //add item to next spot in array
case 0:

}

switch(variable){
case 1:
case 2://all cases
    //add items to next spot in array
    break;
case 1:
case 2:
    if(other factors2)
        //add item to next spot in array
}
这应该符合你的要求

然后,我将用自己的方法提取每个开关块,以使其更易于理解和阅读

你可以考虑把所有这些都提取到一个小类HIRACHY中:

class DefaultExecutor{
    void do(){
        step1();
        step2();
        step3();
        step4();
    }
    void step1(){//all cases class of the first switch statement}
    //... similar methods for the other switcht statements
}

class CaseZeor extends DefaultExecutor{
    // override step1-4 as required for special treatment of case 0
}

// ... further classes for cases 1-3