2015-01-13 163 views
0

我有這段代碼,其中有兩個循環'我'和'j'。嵌套循環無法正確迭代

'j'是內循環變量,它應該在外循環的單次運行時運行999到100。

但它運行隨機等, 假設我= 999

J = 912

J = 911

J = 910

J = 909

J-908

j = 907

J = 906

然後突然內環退出,decreament 1從外循環,使I = 998

然後啓動 'J' 環

J = 908

J = 907

J = 906

J = 905

J = 906

J = 905

J = 904

J = 903

J = 902

然後退出innerloop ......... .......

int product=0; 
    mainloop: 
    for(int i=999;i>99;i--){ 
     for(int j=999;j>99;j--){ 
      boolean flag= doSomething(i*j); 
      if(flag){ 
       product=i*j; 
       System.out.println("Digits are: "+i+" and "+j); 
       break mainloop; 
      } 
     } 
    } 

public boolean doSomething(int product){ 
     String original= Integer.toString(product),reverse=""; 
     int length = original.length(); 
     for (int i = length - 1; i >= 0; i--) 
      reverse = reverse + original.charAt(i); 
     return (original.equals(reverse)); 


    } 

爲什麼會發生這種情況?爲什麼內循環不能完成從999到100的循環?

編輯:要清除,「標誌」將是真實的,只有當j = 913和I = 993,這是主要的問題,這個循環不會在這一點上突破,因爲「J」還沒有出現,但913它會隨機生成數字。而且,'break'不僅會打破主循環,而且還會打破內循環。

+0

看看doSomething中的代碼。這應該清除它 – Stultuske

+0

你明確地擺脫它,並回到'if'結尾的主循環... – Mureinik

+0

你可以顯示'doSomething'的代碼? – Eran

回答

3

您在內部循環中有一個條件 - if(flag) - 如果爲true,則從該循環中斷開。這是內部循環未完成的原因。它不會「隨機」運行。它的行爲取決於doSomething(i*j)方法調用的結果。

運行一些額外的調試打印代碼和doSomething實施你要求有:

int product=0; 
    mainloop: 
    for(int i=999;i>99;i--){ 
    System.out.print ("\n"+i+":"); 
    for(int j=999;j>99;j--){ 
     System.out.print (j+","); 
     boolean flag= doSomething(i*j); 
     if(flag){ 
     product=i*j; 
     System.out.println("\nDigits are: "+i+" and "+j); 
     break mainloop; 
     } 
    } 
    } 

    public static boolean doSomething(int k) 
    { 
     return k == 913*993; 
    } 

給出了預期的輸出:

999:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
998:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
997:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
996:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
995:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
994:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,...,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100, 
993:999,998,997,996,995,994,993,992,991,990,989,988,987,986,985,984,983,982,981,980,979,978,977,976,975,974,973,972,971,970,969,968,967,966,965,964,963,962,961,960,959,958,957,956,955,954,953,952,951,950,949,948,947,946,945,944,943,942,941,940,939,938,937,936,935,934,933,932,931,930,929,928,927,926,925,924,923,922,921,920,919,918,917,916,915,914,913, 
Digits are: 993 and 913 

編輯,doSomething你貼返回true,當我== 995和j == 583。那是當你打破循環。

+0

只有當j = 913和i = 993時,'flag'纔會爲真,這是循環在這一點上不會中斷的主要問題,因爲j永遠不會到913,但它會隨機生成數字。和'break'將會破壞主循環,而不僅僅是內循環。 –

+0

代碼在問題中更新。 –

+0

@HappyDev編輯了答案。 – Eran