将if-else语句更改为switch语句

时间:2015-07-14 15:29:30

标签: java if-statement switch-statement

我在如何处理太多条件方面遇到麻烦。现在我有5个要过滤的项目,并计划添加更多项目,但是当我添加一些项目时,它增加了可能的组合,例如1 * 2 * 3 * 4 * 5组合5,所以当我现在有10个项目时,它会是300万组合。在我目前的线程中,有些人建议我使用开关,但不知道我将如何执行它以减少可能的组合。任何人都可以更改我的if语句切换或有其他方式我可以处理这些组合。这是我的程序

        // ALL filter selected
        for(int subloop=0; subloop<sSpecialty[loop].length; subloop++) {
        if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);
            // RegAffil
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*") 
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Administration
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*") 
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // RoomAmbience
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Tuition Fee
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

            // Specialty
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
                list.add(sList[loop]);  



            // regAffil and admin 0-1
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // regAffil and type 0-2
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // regaffil and ambience 0-3
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // reg afill and tuition 0-4
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // reg afill and specialty 0-5
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);


            // admin and type 1-2
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and ambience 1-3
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and tuition 1-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // admin and specialty 1-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
                            list.add(sList[loop]);



            // type and ambience 2-3
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // type and tuition  2-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // type and specialty 2-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);



            // ambience and tuition 3-4
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
            // ambience and specialty 3-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);

            // tuition and specialty 4-5
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
            list.add(sList[loop]);


            // without specialty

        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")
                ) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && thisFilter[1].equalsIgnoreCase("*")
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (thisFilter[0].equalsIgnoreCase("*")
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);
        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
                && amountTF == 0
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

        } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
                && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
                && thisFilter[3].equalsIgnoreCase("*")
                && amountTF >= theTuitionFee
                && thisFilter[4].equalsIgnoreCase("*")) {
            list.add(sList[loop]);

        }


        // with specialty
     else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])
            ) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && thisFilter[1].equalsIgnoreCase("*")
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (thisFilter[0].equalsIgnoreCase("*")
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && sAmbience[loop].equalsIgnoreCase(thisFilter[3])
            && amountTF == 0
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);

    } else if (sReg[loop].equalsIgnoreCase(thisFilter[0])
            && sAdmin[loop].equalsIgnoreCase(thisFilter[1])
            && thisFilter[3].equalsIgnoreCase("*")
            && amountTF >= theTuitionFee
            && sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4])) {
        list.add(sList[loop]);
    }

2 个答案:

答案 0 :(得分:2)

首先,我在你的ifs中看到了很多重复的陈述。

我会在'优化'的第一次迭代中将所有语句放在这样的变量中:

boolean filterZero = thisFilter[0].equalsIgnoreCase("*");
boolean adminLoop = sAdmin[loop].equalsIgnoreCase(thisFilter[1]);
boolean filterThree = thisFilter[3].equalsIgnoreCase("*"); 
boolean filterFour = thisFilter[4].equalsIgnoreCase("*");

然后将其转换为更具可读性的内容,如:

 else if (filterZero && adminLoop && filterThree && amountTF == 0 
          && filterFour) {
            list.add(sList[loop]);
}

此外,我会尝试查看在这种情况下你不想做list.add(sList[loop]);的情况更少,你可以简化你的条件。

如果不是这样,还要检查合并的条件。例如,如果filterZerofilterThree在使用时始终处于这种状态,那么您可以像这样对它们进行连接:

boolean filterZeroThree = thisFilter[0].equalsIgnoreCase("*") 
                           && thisFilter[3].equalsIgnoreCase("*");

答案 1 :(得分:1)

我建议您首先提取ifs的个别条件。如果我是对的,我发现了这十个条件:

        boolean cond1 = sReg[loop].equalsIgnoreCase(thisFilter[0]);
        boolean cond2 = sAdmin[loop].equalsIgnoreCase(thisFilter[1]);
        boolean cond3 = sAmbience[loop].equalsIgnoreCase(thisFilter[3]);
        boolean cond4 = amountTF >= theTuitionFee;
        boolean cond5 = sSpecialty[loop][subloop].equalsIgnoreCase(thisFilter[4]);
        boolean cond6 = thisFilter[1].equalsIgnoreCase("*");
        boolean cond7 = thisFilter[3].equalsIgnoreCase("*");
        boolean cond8 = amountTF == 0;
        boolean cond9 = thisFilter[4].equalsIgnoreCase("*");
        boolean cond10 = thisFilter[0].equalsIgnoreCase("*");

(你最好给这些条件提供有意义的名字)

对于ifs本身,因为它们都归结为list.add(sList[loop]),你可以尝试简化布尔表达式(假设你有超过4个原始输入,我建议Quine–McCluskey algorithm

同时观察上述某些情况可能是自我排除的(例如,如果theTuitionFee可能为零,我就不知道;如果不是,那么cond4和{{ 1}}不能同时为真。)