2009-09-08 307 views
0

我有我的揮杆接口的Java while循環不循環

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


    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

你真的需要重新思考你的設計。這也難怪代碼不起作用。 – ChaosPandion

+0

ChaosPandion我知道,剛剛得到了第一個設計模式,gona很快就開始閱讀,但它不回答我的問題 – kokokok

回答

5

你提到你想保持循環,直到所有的比較爲真。

如果任何條件成立,現在編碼的方式將退出循環。

不是

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

嘗試

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

+1閱讀所有。 – seth

+0

德摩根法律FTW! :) http://en.wikipedia.org/wiki/De_Morgans_laws – mohaps

0

我的第一個猜測是,它看起來像你在循環中使用按位'&',而不是邏輯比較'& &'。

+0

實際上我使用的是&&但while循環不工作,所以我想我會試一試 – kokokok

+3

@ kokokok讀完最後一句話後,我的大腦翻了個身。請永遠不要那樣做。解決問題有幾個階段,第一個階段總是找出問題所在並理解它。通過任意改變你不明白的東西,你幾乎肯定會讓事情變得更糟。在這種情況下,您可能已經引入了一個錯誤。 – Imagist

+0

哈哈yessir點注意到 – kokokok

0

我的猜測是JVM到達了文本牆,而且和我們很像,不知道該如何處理它,也沒有任何願望。

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

但是說實話,如果你正在循環和應用一個巨大的分支 - 如果階梯,你做了一些非常嚴重的錯誤。即使你得到它的工作,這種代碼是在處理器上的地獄,更不用說是一個痛苦的維護。我的建議是:重構。

1

也許你的up/down/left/right/left/leftinv等方法正在修改你正在測試顏色的背景,導致while條件評估爲false,從而防止超過1迭代。

+0

該死的感覺非常明顯,現在我覺得自己在面對自己。 ty – kokokok