0

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

    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

4 回答 4

5

您提到要继续循环,直到所有比较都为真。

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

而不是

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

尝试

while ( ! (boolean1 && boolean2 && boolean3) ) {
于 2009-09-08T03:44:03.327 回答
1

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

于 2009-09-08T03:19:20.847 回答
0

我的第一个猜测是,您似乎在循环中使用的是按位“&”,而不是逻辑比较“&&”。

于 2009-09-08T03:12:28.730 回答
0

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

那,或者你正在使用&而不是&&.

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

于 2009-09-08T03:17:45.297 回答