在Java中使用循环优化if语句

时间:2013-04-04 18:38:03

标签: java loops

我正在尝试优化此代码中存在的大量if语句:

public class MyClass{
    ArrayList<Stack<String>> positions = new ArrayList<Stack<String>>();

    Stack<String> pos1;
    Stack<String> pos2;
    Stack<String> pos3;
    Stack<String> pos4;
    Stack<String> pos5;
    Stack<String> pos6;
    Stack<String> pos7;
    Stack<String> pos8;
    Stack<String> pos9;
    Stack<String> pos10;
    Stack<String> pos11;
    Stack<String> pos12;
    Stack<String> pos13;
    Stack<String> pos14;
    Stack<String> pos15;
    Stack<String> pos16;
    Stack<String> pos17;
    Stack<String> pos18;
    Stack<String> pos19;
    Stack<String> pos20;
    Stack<String> pos21;
    Stack<String> pos22;
    Stack<String> pos23;
    Stack<String> pos24;
    Stack<String> pos25;
    Stack<String> pos26;
    Stack<String> pos27;

public ClassName{
        for (int i=0; i<28; i++) {
            positions.add(i,new Stack<String>());
        }


}

public void Updater (int position, boolean player){


        if(player){ // if player one is up
            // start position 1 actions
            if(position==1){
                int distance = pos1.size(); 
                if(distance==1){
                    String temp = pos1.peek();
                    if(temp=="x"){ // only make move if player one's chip is on top of the stack
                        pos1.pop();
                        pos2.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos3.push(temp);
                    }
                }
                if(distance==3){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos7.push(temp);
                    }
                }
                if(distance==4){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos8.push(temp);
                    }
                }
                if(distance==5){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos9.push(temp);
                    }
                }
                if(distance==6){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos10.push(temp);
                    }
                }
                if(distance==7){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos11.push(temp);
                    }
                }
                if(distance==8){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos12.push(temp);
                    }
                }
                if(distance==9){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos13.push(temp);
                    }
                }
                if(distance==10){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos14.push(temp);
                    }
                }
                if(distance==11){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos15.push(temp);
                    }
                }
                if(distance==12){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                if(distance==13){
                    String temp = pos1.peek();
                    if(temp=="x"){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                if(distance>13){
                    String temp = pos1.peek();
                    pos1.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 1 actions

            // start position 2 actions
            if(position==2){
                int distance = pos2.size(); 
                if(distance==1){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos3.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos7.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos2.peek();
                    if(temp=="x"){ 
                        pos2.pop();
                        pos8.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos9.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos10.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos11.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos12.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos2.peek();
                    if(temp=="x"){ 
                        pos2.pop();
                        pos13.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos14.push(temp);
                    }
                }
                if(distance==9){
                    String temp = pos2.peek();
                    if(temp=="x"){
                        pos2.pop();
                        pos15.push(temp);
                    }
                }
                if(distance==10){
                    String temp = pos2.peek();
                    if(temp=="x"){ 
                        pos2.pop();
                        pos16.push(temp);
                    }
                }
                if(distance==11){
                    String temp = pos2.peek();
                    if(temp=="x"){ 
                        pos2.pop();
                        pos17.push(temp);
                    }
                }
                if(distance>11){
                    String temp = pos2.peek();
                    pos2.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 2 actions

            // start position 3 actions
            if(position==3){
                int distance = pos3.size(); 
                if(distance==1){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos7.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos8.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos3.peek();
                    if(temp=="x"){ 
                        pos3.pop();
                        pos9.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos10.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos11.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos12.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos13.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos3.peek();
                    if(temp=="x"){ 
                        pos3.pop();
                        pos14.push(temp);
                    }
                }           
                if(distance==9){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos15.push(temp);
                    }
                }
                if(distance==10){
                    String temp = pos3.peek();
                    if(temp=="x"){
                        pos3.pop();
                        pos16.push(temp);
                    }
                }
                if(distance==11){
                    String temp = pos3.peek();
                    if(temp=="x"){ 
                        pos3.pop();
                        pos17.push(temp);
                    }
                }
                if(distance>11){
                    String temp = pos3.peek();
                    pos3.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 3 actions

            // start position 7 actions
            if(position==7){
                int distance = pos7.size(); 
                if(distance==1){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos8.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos9.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos7.peek();
                    if(temp=="x"){ 
                        pos7.pop();
                        pos10.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos11.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos12.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos13.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos14.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos7.peek();
                    if(temp=="x"){ 
                        pos7.pop();
                        pos15.push(temp);
                    }
                }           
                if(distance==9){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos16.push(temp);
                    }
                }
                if(distance==10){
                    String temp = pos7.peek();
                    if(temp=="x"){
                        pos7.pop();
                        pos17.push(temp);
                    }
                }
                if(distance>10){
                    String temp = pos7.peek();
                    pos7.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 7 actions

            // start position 8 actions
            if(position==8){
                int distance = pos8.size(); 
                if(distance==1){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos9.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos10.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos8.peek();
                    if(temp=="x"){ 
                        pos8.pop();
                        pos11.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos12.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos13.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos14.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos15.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos8.peek();
                    if(temp=="x"){ 
                        pos8.pop();
                        pos16.push(temp);
                    }
                }           
                if(distance==9){
                    String temp = pos8.peek();
                    if(temp=="x"){
                        pos8.pop();
                        pos17.push(temp);
                    }
                }
                if(distance>9){
                    String temp = pos8.peek();
                    pos8.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 8 actions

            // start position 9 actions
            if(position==9){
                int distance = pos9.size(); 
                if(distance==1){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos10.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos11.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos9.peek();
                    if(temp=="x"){ 
                        pos9.pop();
                        pos12.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos13.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos14.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos15.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos9.peek();
                    if(temp=="x"){
                        pos9.pop();
                        pos16.push(temp);
                    }
                }           
                if(distance==8){
                    String temp = pos9.peek();
                    if(temp=="x"){ 
                        pos9.pop();
                        pos17.push(temp);
                    }
                }           
                if(distance>8){
                    String temp = pos9.peek();
                    pos9.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 9 actions

            // start position 10 actions
            if(position==10){
                int distance = pos10.size();    
                if(distance==1){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos11.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos12.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos10.peek();
                    if(temp=="x"){ 
                        pos10.pop();
                        pos13.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos14.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos15.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos16.push(temp);
                    }
                }               
                if(distance==7){
                    String temp = pos10.peek();
                    if(temp=="x"){
                        pos10.pop();
                        pos17.push(temp);
                    }
                }                   
                if(distance>7){
                    String temp = pos10.peek();
                    pos10.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 10 actions

            // start position 11 actions
            if(position==11){
                int distance = pos11.size();    
                if(distance==1){
                    String temp = pos11.peek();
                    if(temp=="x"){
                        pos11.pop();
                        pos12.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos11.peek();
                    if(temp=="x"){
                        pos11.pop();
                        pos13.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos11.peek();
                    if(temp=="x"){ 
                        pos11.pop();
                        pos14.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos11.peek();
                    if(temp=="x"){
                        pos11.pop();
                        pos15.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos11.peek();
                    if(temp=="x"){
                        pos11.pop();
                        pos16.push(temp);
                    }
                }               
                if(distance==6){
                    String temp = pos11.peek();
                    if(temp=="x"){
                        pos11.pop();
                        pos17.push(temp);
                    }
                }                   
                if(distance>6){
                    String temp = pos11.peek();
                    pos11.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 11 actions

            // start position 12 actions
            if(position==12){
                int distance = pos12.size();    
                if(distance==1){
                    String temp = pos12.peek();
                    if(temp=="x"){
                        pos12.pop();
                        pos13.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos12.peek();
                    if(temp=="x"){
                        pos12.pop();
                        pos14.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos12.peek();
                    if(temp=="x"){ 
                        pos12.pop();
                        pos15.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos12.peek();
                    if(temp=="x"){
                        pos12.pop();
                        pos16.push(temp);
                    }
                }           
                if(distance==5){
                    String temp = pos12.peek();
                    if(temp=="x"){
                        pos12.pop();
                        pos17.push(temp);
                    }
                }                               
                if(distance>5){
                    String temp = pos12.peek();
                    pos12.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 12 actions

            // start position 13 actions
            if(position==13){
                int distance = pos13.size();    
                if(distance==1){
                    String temp = pos13.peek();
                    if(temp=="x"){
                        pos13.pop();
                        pos14.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos13.peek();
                    if(temp=="x"){
                        pos13.pop();
                        pos15.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos13.peek();
                    if(temp=="x"){ 
                        pos13.pop();
                        pos16.push(temp);
                    }
                }               
                if(distance==4){
                    String temp = pos13.peek();
                    if(temp=="x"){
                        pos13.pop();
                        pos17.push(temp);
                    }
                }                                       
                if(distance>4){
                    String temp = pos13.peek();
                    pos13.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 13 actions

            // start position 14 actions
            if(position==14){
                int distance = pos14.size();    
                if(distance==1){
                    String temp = pos14.peek();
                    if(temp=="x"){
                        pos14.pop();
                        pos15.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos14.peek();
                    if(temp=="x"){
                        pos14.pop();
                        pos16.push(temp);
                    }
                }               
                if(distance==3){
                    String temp = pos14.peek();
                    if(temp=="x"){ 
                        pos14.pop();
                        pos17.push(temp);
                    }
                }                                                   
                if(distance>3){
                    String temp = pos14.peek();
                    pos14.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 14 actions

            // start position 15 actions
            if(position==15){
                int distance = pos15.size();    
                if(distance==1){
                    String temp = pos15.peek();
                    if(temp=="x"){
                        pos15.pop();
                        pos16.push(temp);
                    }
                }
                if(distance==2){
                    String temp = pos15.peek();
                    if(temp=="x"){
                        pos15.pop();
                        pos17.push(temp);
                    }
                }                                           
                if(distance>2){
                    String temp = pos15.peek();
                    pos15.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 15 actions

            // start position 16 actions
            if(position==15){
                int distance = pos16.size();    
                if(distance==1){
                    String temp = pos16.peek();
                    if(temp=="x"){
                        pos16.pop();
                        pos17.push(temp);
                    }
                }                                   
                if(distance>1){
                    String temp = pos16.peek();
                    pos16.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 16 actions

            // start position 17 actions
            if(position==17){
                int distance = pos17.size();                                    
                if(distance>=1){
                    String temp = pos17.peek();
                    pos17.pop();
                    if(temp=="x"){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 17 actions

            player = false;
            frame.player.setText("Current Player's Turn: Player Two");
        }
}

}

基本上,有25个堆栈,每个堆栈存储x和o的各种字符串。 updater方法根据参数将x和o从一个堆栈移动到另一个堆栈。是否真的有必要编写尽可能多的if语句来覆盖每个场景?是否有一个我可以使用的循环?

x和o只能向前移动,而不是向后移动。

6 个答案:

答案 0 :(得分:4)

为什么不创建堆栈数组?然后你可以减少if语句:

Stack[] stacks = new Stack[27];
Arrays.fill(stacks, new Stack<String>);
if (temp.equals("x")) {
   Stack stack = stacks[distance];
   //all other stuff
}

答案 1 :(得分:1)

声明:if(distance==1){ 可以用这样的东西代替:

for(int i= 0 ;i< 13;i++) {
        if(distance==i) {
            // some code
            break; // break the loop once match is found
        }
    }

答案 2 :(得分:0)

如果您想进行优化,可以在90%的重新审核时使用else if开始判断。这样你只能打到你需要的而不是全部。这也会减少烦恼。

if(player){ // if player one is up
            // start position 1 actions
            if(position==1){
                int distance = pos1.size(); 
                if(distance==1){
                    String temp = pos1.peek();
                    if(temp.equals("x")){ // only make move if player one's chip is on top of the stack
                        pos1.pop();
                        pos2.push(temp);
                    }
                }
               else if(distance==2){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos3.push(temp);
                    }
                }
               else if(distance==3){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos7.push(temp);
                    }
                }
                else if(distance==4){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos8.push(temp);
                    }
                }
                else if(distance==5){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos9.push(temp);
                    }
                }
                else if(distance==6){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos10.push(temp);
                    }
                }
                else if(distance==7){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos11.push(temp);
                    }
                }
                else if(distance==8){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos12.push(temp);
                    }
                }
                else if(distance==9){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos13.push(temp);
                    }
                }
                else if(distance==10){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos14.push(temp);
                    }
                }
                else if(distance==11){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos15.push(temp);
                    }
                }
                else if(distance==12){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                else if(distance==13){
                    String temp = pos1.peek();
                    if(temp.equals("x")){
                        pos1.pop();
                        pos16.push(temp);
                    }
                }
                else if(distance>13){
                    String temp = pos1.peek();
                    pos1.pop();
                    if(temp.equals("x")){
                        pos26.push(temp); // push to player one's winners circle
                    }
                }

            }
            // end position 1 actions

答案 3 :(得分:0)

您可以使用嵌套的for循环,然后if根据i的值进行测试:

for(int i = 1;i<=15;i++)//position
{
for(int c = 1;c<=4;c++)//distance
{
if(position==i)//etc.
}
}

答案 4 :(得分:0)

您需要做的事情很少:

如果您发现自己做了很多事情if(distance==1){ ... pos1.pop();然后if(distance==2){ ... pos2.pop();等,请使用ArrayList<Stack<String>>你已经宣布,只需得到你需要的那个。

if (/* distance is within bounds */)
{
    Stack<String> pos = positions.get(distance);
    ...

您可以使用您获得的temp值执行类似操作,以确定需要推送的值。这将消除所有重复代码if块。

接下来,在每种情况下,您都会将字符串值与==运算符进行比较。 ==运算符比较引用以查看它们是否相同,这可能不是您想要的。使用String#equals来比较String值。

答案 5 :(得分:0)

以下是一些提示。

动态创建堆栈:

int total = 27
List<Stack<String>> positions = new ArrayList<Stack<String>>();
for (i = 0; i < toal; i++){
   positions.add(new Stack<String>());
}

您现在可以使用positions.get(position);

访问它们

您可以大大简化代码:

Stack<String> positionStack = positions.get(position);
int distance = positionStack.size();
if (positionStack.peek().equals("x")){
   positions(nextMove(position).push(positionStack.pop());
}

nextMove(int position)是一个计算给定位置下一个位置的函数。我不知道那个功能是什么,但你可以想出来。