简体   繁体   English

Java while循环不循环

[英]Java while loop not looping

I have the following button in my swing interface 我的挥杆界面中有以下按钮

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


    step1(); 


}   

which calls 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

The problem seems to be the outermost while loop in step1(). 这个问题似乎是step1()中最外层的while循环。 It is supposed to keep running until all the condition are met but it just stops after one iteration and I can't figure out why. 它应该一直运行,直到满足所有条件为止,但是在一次迭代之后它就停止了,我不知道为什么。 If I remove the outer while loop step1() proceeds exactly as it should going through each step on multiple presses of the button. 如果我移除外部while循环,则step1()会完全按多次按下按钮的每一步进行。 Help! 救命!

You mention that you want to keep looping until all the comparisons are true. 您提到要一直循环直到所有比较都成立。

The way it's coded now will drop out of the loop if any condition becomes true. 如果任何条件变为真,则现在的编码方式将退出循环。

Rather than 而不是

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

Try 尝试

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

也许您的up / down / left / right / left / leftinv等方法之一正在修改测试颜色的背景,导致while条件的值为false,从而防止了1次以上的迭代。

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

My guess is that the JVM reaches that wall of text and, much like us, has no idea what to do with it, nor any desire to. 我的猜测是,JVM到达了那堵墙,就像我们一样,JVM也不知道该如何处理,也不希望这样做。

That, or you're using & instead of && . 那,或者您使用&而不是&&

But honestly, if you're looping through and applying a gigantic branching else-if ladder, you've done something pretty severely wrong. 但老实说,如果您遍历并应用了一个巨大的其他分支(如果是梯子),那么您所做的事情就非常错误了。 Even if you get it to work, this kind of code is hell on the processor, not to mention being a pain to maintain. 即使您可以正常工作,这种代码也很难在处理器上处理,更不用说要维护了。 My recommendation: refactor. 我的建议:重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM