是否可以缩短if-else-if语句,我该怎么做?

时间:2017-05-16 21:36:20

标签: java if-statement

我创建了这个if-else-if语句,并且想知道我是否能够缩短它以便它不会占用太多空间。如果有人知道如何做到这一点,请帮忙!

if (seatNum <= 20) System.out.println("Row 1");
else if (seatNum > 20 && seatNum <= 40) System.out.println("Row 2");
else if (seatNum > 40 && seatNum <= 60) System.out.println("Row 3");
else if (seatNum > 60 && seatNum <= 80) System.out.println("Row 4");
else if (seatNum > 80 && seatNum <= 100) System.out.println("Row 5");
else if (seatNum > 100 && seatNum <= 120) System.out.println("Row 6");
else if (seatNum > 120 && seatNum <= 140) System.out.println("Row 7");
else if (seatNum > 140 && seatNum <= 160) System.out.println("Row 8");
else if (seatNum > 160 && seatNum <= 180) System.out.println("Row 9");
else if (seatNum > 180 && seatNum <= 201) System.out.println("Row 10");
else if (seatNum > 201 && seatNum <= 216) System.out.println("Row 11");
else if (seatNum > 216 && seatNum <= 231) System.out.println("Row 12");
else if (seatNum > 231 && seatNum <= 246) System.out.println("Row 13");
else if (seatNum > 246 && seatNum <= 261) System.out.println("Row 14");
else if (seatNum > 261 && seatNum <= 276) System.out.println("Row 15");
else if (seatNum > 276 && seatNum <= 291) System.out.println("Row 16");
else if (seatNum > 291 && seatNum <= 306) System.out.println("Row 17");
else if (seatNum > 306 && seatNum <= 321) System.out.println("Row 18");
else if (seatNum > 321 && seatNum <= 336) System.out.println("Row 19");
else if (seatNum > 336 && seatNum <= 351) System.out.println("Row 20");

编辑:包括座位201.抱歉所有的混乱!

10 个答案:

答案 0 :(得分:9)

我们可以使用 <div class="container"> <div class="row"> <div class="col-md-12"><div style="background:black;height:100px;"></div></div> </div> - Java中的分区总是向零舍入的事实,这意味着int5 / 2 == 2

对于-5 /2 == -2,行始终为seatNum <= 0。 只要1,行就是seatNum <= 200。如果是((seatNum - 1) / 20) + 1,我们只需> 201即可。我们减去((seatNum - 202) / 15) + 11因为数字是由202设定的:2仍在行216中,11在行217中。同样适用于12231。通过此计算,未定义的座位232将位于行201中,但忽略此输入或抛出11并不重要。

Exception

如果行变得更复杂/不规则,我可能会稍微调整一下Dale's solution。这似乎具有可读性和可扩展性。

答案 1 :(得分:4)

int highestRowSeat[] = {20, 40, 80, 100, 120, 140, 160, 180, 200, 216, 231, 246, 261, 276, 306, 321, 336, 351};

for (int row = 1; row <= highestRowSeat.length; ++row) {
    if (seatNum <= highestRowSeat[row-1]) {
        System.out.println("Row " + row);
        break;
    }
}

答案 2 :(得分:3)

您可以减少的一件事是检查的数量,在这种情况下,因为您使用if...else,只有前一个条件为false时才会检查以下条件,因此您可以删除更大的而不是检查每个else if,因为它将由前一个自动检查。

编辑:添加了对缺失的201

的额外检查
if (seatNum <= 20) System.out.println("Row 1");
else if (seatNum <= 40) System.out.println("Row 2");
else if (seatNum <= 60) System.out.println("Row 3");
else if (seatNum <= 80) System.out.println("Row 4");
else if (seatNum <= 100) System.out.println("Row 5");
else if (seatNum <= 120) System.out.println("Row 6");
else if (seatNum <= 140) System.out.println("Row 7");
else if (seatNum <= 160) System.out.println("Row 8");
else if (seatNum <= 180) System.out.println("Row 9");
else if (seatNum <= 200) System.out.println("Row 10");
else if (seatNum <= 216 && seatNum != 201) System.out.println("Row 11");
else if (seatNum <= 231) System.out.println("Row 12");
else if (seatNum <= 246) System.out.println("Row 13");
else if (seatNum <= 261) System.out.println("Row 14");
else if (seatNum <= 276) System.out.println("Row 15");
else if (seatNum <= 291) System.out.println("Row 16");
else if (seatNum <= 306) System.out.println("Row 17");
else if (seatNum <= 321) System.out.println("Row 18");
else if (seatNum <= 336) System.out.println("Row 19");
else if (seatNum <= 351) System.out.println("Row 20");

答案 3 :(得分:2)

是的,确定

String s = "";
System.out.print("Row No. ");

if      (seatNum <=  20) s = "1";
else if (seatNum <=  40) s = "2";
else if (seatNum <=  60) s = "3";
else if (seatNum <=  80) s = "4";
else if (seatNum <= 100) s = "5";
else if (seatNum <= 120) s = "6";
else if (seatNum <= 140) s = "7";
else if (seatNum <= 160) s = "8";
else if (seatNum <= 180) s = "9";
else if (seatNum <= 200) s = "10";
else if (seatNum <= 216) s = "11";
else if (seatNum <= 231) s = "12";
else if (seatNum <= 246) s = "13";
else if (seatNum <= 261) s = "14";
else if (seatNum <= 276) s = "15";
else if (seatNum <= 291) s = "16";
else if (seatNum <= 306) s = "17";
else if (seatNum <= 321) s = "18";
else if (seatNum <= 336) s = "19";
else if (seatNum <= 351) s = "20";

System.out.println(s);

答案 4 :(得分:2)

值得探索的两个选项是使用TreeMap和二分搜索。

使用TreeMap,您可以执行以下操作:

private static final TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(20, 1);
map.put(40, 2);
//etc

然后在你的方法中(根据需要添加空检查):

int row = map.ceilingEntry(seatNum).getValue();

二进制搜索选项如下所示:

private static final int[] rowEnds = { 20, 40, ... };

(该数组需要排序)并在您的方法中:

int rowIndex = Arrays.binarySearch(rowEnds, seatNum);
if (rowIndex < 0) rowIndex = - rowIndex + 1;
int row = rowEnds[rowIndex];

答案 5 :(得分:0)

这个怎么样......

pip

答案 6 :(得分:0)

我想提一下Conditional Operator ? :。虽然 NOT 可以节省大量空间,但是要记住这一点很好,因为一些程序员认为它更具可读性且一点点 {{ 3}}执行。

String row = (seatNum <= 20)  ? "Row 1"  : (seatNum <= 40)  ? "Row 2"  :
             (seatNum <= 60)  ? "Row 3"  : (seatNum <= 80)  ? "Row 4"  :
             (seatNum <= 100) ? "Row 5"  : (seatNum <= 120) ? "Row 6"  :
             (seatNum <= 140) ? "Row 7"  : (seatNum <= 160) ? "Row 8"  :
             (seatNum <= 180) ? "Row 9"  : (seatNum <= 200) ? "Row 10" :
             (seatNum <= 216) ? "Row 11" : (seatNum <= 231) ? "Row 12" :
             (seatNum <= 246) ? "Row 13" : (seatNum <= 261) ? "Row 14" :
             (seatNum <= 276) ? "Row 15" : (seatNum <= 291) ? "Row 16" :
             (seatNum <= 306) ? "Row 17" : (seatNum <= 321) ? "Row 18" :
             (seatNum <= 336) ? "Row 19" : (seatNum <= 351) ? "Row 20" : null;

System.out.println(row); // to see the result

答案 7 :(得分:0)

if (seatNum <= 200) {
    System.out.println("Row "+ ceil(seatNum/20));
}
else if (seatNum <= 216) {
    System.out.println("Row "+ ceil((seatNum-45)/16));
}
else if (seatNum > 216) {
    System.out.println("Row "+ ceil((seatNum-49)/15));
}

我不知道正确的函数名称或Java的任何内容,但其逻辑运作

答案 8 :(得分:0)

你可以写一下:

System.out.println("Row " + ((seatNum <= 200)?
                            ((Math.max(20, seatNum) - 1) / 20) + 1 
                          : ((seatNum - 202) / 15) + 11));

我忽略了可能是错误的201案例(必要时将202更改为201。)

答案 9 :(得分:0)

我们为您的代码选择一个函数(){},输出为

在这个想法中,

public static void main(String[] args) {
    ArrayList<Integer>index=new ArrayList<Integer>();
    ArrayList<String>excep=new ArrayList<String>();
    for (int i = 0; i < 400; i++) {
        if(!seat(i).equals(seat(i-1))){
            excep.add(seat(i-1));
            index.add(i);
        }
    }

    for (int test = 0; test < 400; test++) {
        if(test<352){
            int i=0;
            for(;i<index.size();i++){
                if(index.get(i)-test>=0){
                    if(index.get(i)-test==0)
                        i++;
                    break;
                }   
            }
            System.out.println(excep.get(i));;  
        }else if(test==201){
            System.out.println(seat(test));
        }else
            System.out.println("null");
    }       

}

public static String seat(int seatNum) {
    if (seatNum <= 20)
        return ("Row 1");
    else if (seatNum <= 40)
        return ("Row 2");
    else if (seatNum <= 60)
        return ("Row 3");
    else if (seatNum <= 80)
        return ("Row 4");
    else if (seatNum <= 100)
        return ("Row 5");
    else if (seatNum <= 120)
        return ("Row 6");
    else if (seatNum <= 140)
        return ("Row 7");
    else if (seatNum <= 160)
        return ("Row 8");
    else if (seatNum <= 180)
        return ("Row 9");
    else if (seatNum <= 200)
        return ("Row 10");
    else if (seatNum <= 216)
        return ("Row 11");
    else if (seatNum <= 231)
        return ("Row 12");
    else if (seatNum <= 246)
        return ("Row 13");
    else if (seatNum <= 261)
        return ("Row 14");
    else if (seatNum <= 276)
        return ("Row 15");
    else if (seatNum <= 291)
        return ("Row 16");
    else if (seatNum <= 306)
        return ("Row 17");
    else if (seatNum <= 321)
        return ("Row 18");
    else if (seatNum <= 336)
        return ("Row 19");
    else if (seatNum <= 351)
        return ("Row 20");
    return "null";
}