Java while循环不循环

时间:2009-09-08 03:09:14

标签: java while-loop

我的摇摆界面中有以下按钮

    private void solveButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         


    step1(); 


}   

调用step1();

   private void step1(){

//step 1 solving the white cross around the yellow middle
        while( (!(F2.getBackground().equals(white))) & (!(F4.getBackground().equals(white))) & (!(F6.getBackground().equals(white))) & (!(F8.getBackground().equals(white)))){


         textArea.append("cnt");

        //top three pieces solved
        if(F2.getBackground().equals(white) && F4.getBackground().equals(white) && F6.getBackground().equals(white)){
            //solve F8 
            while(!(F8.getBackground().equals(white))){

                //check right side
                if(R2.getBackground().equals(white)){right(); right(); downinv(); rightinv(); rightinv();}
                else if(R4.getBackground().equals(white)){rightinv(); downinv(); right();}
                else if(R6.getBackground().equals(white)){right(); downinv(); rightinv();}
                else if(R8.getBackground().equals(white)){downinv();}
                //check left side
                else if(L2.getBackground().equals(white)){left(); left(); down(); leftinv(); leftinv();}
                else if(L4.getBackground().equals(white)){leftinv(); down(); left();}
                else if(L6.getBackground().equals(white)){left(); down(); leftinv();}
                else if(L8.getBackground().equals(white)){down();}
                //check back side
                else if(B2.getBackground().equals(white)){back(); back(); down(); down();}
                else if(B4.getBackground().equals(white)){backinv(); down(); down();}
                else if(B6.getBackground().equals(white)){back(); down(); down();}
                else if(B8.getBackground().equals(white)){down(); down();}
                //check up side
                else if(U2.getBackground().equals(white)){frontinv(); up(); rightinv(); upinv();}
                else if(U4.getBackground().equals(white)){frontinv(); up(); up(); rightinv(); upinv(); upinv();}
                else if(U6.getBackground().equals(white)){frontinv(); rightinv();}
                else if(U8.getBackground().equals(white)){front();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); frontinv(); right();}
                else if(D4.getBackground().equals(white)){front(); right();}
                else if(D6.getBackground().equals(white)){frontinv(); leftinv();}
                else if(D8.getBackground().equals(white)){downinv(); frontinv(); right();}
            }//end while
        }//end if

        //top two pieces solved
        if(F2.getBackground().equals(white) && F4.getBackground().equals(white)){
            //solve F6
            while(!(F6.getBackground().equals(white))){
                //chech front side
                if(F8.getBackground().equals(white)){front();}
                //check right side
                else if(R2.getBackground().equals(white)){frontinv(); up(); front();}
                else if(R4.getBackground().equals(white)){right(); frontinv(); up(); front();}
                else if(R6.getBackground().equals(white)){rightinv(); frontinv(); up(); front();}
                else if(R8.getBackground().equals(white)){right(); right(); frontinv(); up(); front();}
                //check left side
                else if(L2.getBackground().equals(white)){frontinv(); upinv(); front();}
                else if(L4.getBackground().equals(white)){frontinv(); left(); upinv(); leftinv(); front();}
                else if(L6.getBackground().equals(white)){frontinv(); leftinv(); upinv(); left(); front();}
                else if(L8.getBackground().equals(white)){frontinv(); left(); left(); upinv(); left(); left(); front();}
                //check back side
                else if(B2.getBackground().equals(white)){frontinv(); up(); up(); front();}
                else if(B4.getBackground().equals(white)){frontinv(); backinv(); up(); up(); front();}
                else if(B6.getBackground().equals(white)){frontinv(); back(); up(); up(); front();}
                else if(B8.getBackground().equals(white)){frontinv(); back(); back(); up(); up(); front();}
                //check up side
                else if(U2.getBackground().equals(white)){up();rightinv();upinv();}
                else if(U4.getBackground().equals(white)){up(); up();rightinv(); upinv(); upinv();}
                else if(U6.getBackground().equals(white)){rightinv();}
                else if(U8.getBackground().equals(white)){upinv(); rightinv(); up();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); right();}
                else if(D4.getBackground().equals(white)){down(); down(); right();}
                else if(D6.getBackground().equals(white)){right();}
                else if(D8.getBackground().equals(white)){downinv(); right();}
            }//end while
            textArea.append("\nstep 1a (75% complete)\n");
        }//end if

        //top piece solved
        if( (F2.getBackground().equals(white)) && (!(F4.getBackground().equals(white))) ){
            //solve F4
            while(!(F4.getBackground().equals(white))){
                //check front side
                if(F6.getBackground().equals(white)){frontinv();}
                //check right side
                else if(R2.getBackground().equals(white)){front(); up(); frontinv();}
                else if(R4.getBackground().equals(white)){right(); front(); up(); frontinv();}
                else if(R6.getBackground().equals(white)){rightinv(); front(); up(); frontinv();}
                else if(R8.getBackground().equals(white)){right(); right(); front(); up(); frontinv();}
                //check left side
                else if(L2.getBackground().equals(white)){front(); upinv(); frontinv();}
                else if(L4.getBackground().equals(white)){front(); left(); upinv(); frontinv();}
                else if(L6.getBackground().equals(white)){front(); leftinv(); upinv(); frontinv();}
                else if(L8.getBackground().equals(white)){front(); left(); left(); upinv(); frontinv();}
                //check back side
                else if(B2.getBackground().equals(white)){front(); up(); up(); frontinv();}
                else if(B4.getBackground().equals(white)){front(); backinv(); up(); up(); frontinv();}
                else if(B6.getBackground().equals(white)){front(); back(); up(); up(); frontinv();}
                else if(B8.getBackground().equals(white)){front(); back(); back(); up(); up(); frontinv();}
                //check up side
                else if(U2.getBackground().equals(white)){front(); front(); up(); rightinv(); frontinv(); frontinv();}
                else if(U4.getBackground().equals(white)){left();}
                else if(U6.getBackground().equals(white)){front(); front(); rightinv(); frontinv(); frontinv();}
                else if(U8.getBackground().equals(white)){front(); front(); upinv(); rightinv(); frontinv(); frontinv();}
                //check down side
                else if(D2.getBackground().equals(white)){downinv(); leftinv();}
                else if(D4.getBackground().equals(white)){leftinv();}
                else if(D6.getBackground().equals(white)){downinv(); downinv(); leftinv();}
                else if(D8.getBackground().equals(white)){down(); leftinv();}
            }//end while
            textArea.append("\nstep 1a (50% complete)\n");
        }//end if

        if(!(F2.getBackground().equals(white))){ 
            //solve top piece

          while(!(F2.getBackground().equals(white))){
                if(F4.getBackground().equals(white)){front();}
                else if(F6.getBackground().equals(white)){frontinv();}
                else if(F8.getBackground().equals(white)){front(); front();}
                //check right side
                else if(R2.getBackground().equals(white)){up();}
                else if(R4.getBackground().equals(white)){right(); up();}
                else if(R6.getBackground().equals(white)){rightinv(); up();}
                else if(R8.getBackground().equals(white)){right(); right(); up();}
                //check left side
                else if(L2.getBackground().equals(white)){upinv();}
                else if(L4.getBackground().equals(white)){left(); up();}
                else if(L6.getBackground().equals(white)){leftinv(); up();}
                else if(L8.getBackground().equals(white)){left(); left(); upinv();}
                //check back side
                else if(B2.getBackground().equals(white)){up(); up();}
                else if(B4.getBackground().equals(white)){backinv(); up(); up();}
                else if(B6.getBackground().equals(white)){back(); up(); up();}
                else if(B8.getBackground().equals(white)){back(); back(); up(); up();}
                //check up side
                else if(U2.getBackground().equals(white)){up(); rightinv(); frontinv();}
                else if(U4.getBackground().equals(white)){up(); up(); rightinv(); frontinv();}
                else if(U6.getBackground().equals(white)){rightinv();frontinv();}
                else if(U8.getBackground().equals(white)){upinv(); rightinv(); frontinv();}
                //check down side
                else if(D2.getBackground().equals(white)){down(); right(); frontinv();}
                else if(D4.getBackground().equals(white)){down(); down(); right(); frontinv();}
                else if(D6.getBackground().equals(white)){right(); frontinv();}
                else if(D8.getBackground().equals(white)){downinv(); right(); frontinv();}


           }//end while
           textArea.append("\nStep 1a (25% complete)\n");
        }//end else
    }//end while
}//end step 1

问题似乎是step1()中最外层的while循环。它应该继续运行,直到满足所有条件,但它只是在一次迭代后停止,我无法弄清楚原因。如果我删除外部while循环step1()完全按照按钮的多次按下每个步骤进行。救命啊!

4 个答案:

答案 0 :(得分:5)

你提到你要保持循环,直到所有的比较都成立。

如果任何条件成立,它现在编码的方式将退出循环。

而不是

while ( !boolean1 && !boolean2 && !boolean3) {

尝试

while ( ! (boolean1 && boolean2 && boolean3) ) {

答案 1 :(得分:1)

也许您的上/下/左/右/左/左等等方法中的一个正在修改您正在测试颜色的背景,导致while条件评估为false,从而防止超过1次迭代

答案 2 :(得分:0)

我的第一个猜测是看起来你正在使用按位'&',而不是逻辑比较'&&'在你的循环中。

答案 3 :(得分:0)

我的猜测是,JVM到达了文本墙,就像我们一样,不知道如何处理它,也不知道如何处理。

那,或者您使用&代替&&

但老实说,如果你正在循环并应用一个巨大的分支其他 - 如果阶梯,你做了一些非常严重的错误。即使你让它工作,这种代码在处理器上也是地狱,更不用说维护是一种痛苦。我的建议:重构。