2014-09-22 96 views
0

我遇到了增加4個嵌套while循環的問題。假設你有一個double類型的數組,並且有很多整數來遞增循環。這裏是一個例子遞增多個嵌套雖然循環

private static double[] approxOfU(double w, double x, double y, double z, 
     double u) { 
    double a = 0, b = 0, c = 0, d = 0; 
    final double[] powerArray = { -5.0, -4.0, -3.0, -2.0, -1, (-1.0/2.0), 
      (-1.0/3.0), (-1.0/4.0), 0, (1.0/4.0), (1.0/3.0), 
      (1.0/2.0), 1, 2, 3, 4, 5 }; 
    double[] temp = new double[5]; 
    double approx; 
    int i = 0, j = 0, k = 0, l = 0; 

    while (i < powerArray.length) { 
     a = powerArray[i]; 
     while (j < powerArray.length) { 
      b = powerArray[j]; 
      while (k < powerArray.length) { 
       c = powerArray[k]; 
       while (l < powerArray.length) { 
        d = powerArray[l]; 
        approx = Math.pow(w, a) * Math.pow(x, b) 
          * Math.pow(y, c) * Math.pow(z, d); 
        if (Math.abs(approx - u)/u <= 0.01) { 
         temp[0] = a; 
         temp[1] = b; 
         temp[2] = c; 
         temp[3] = d; 
         temp[4] = approx; 

        } 
        l++; 

       } 
       k++; 
      } 
      j++; 
     } 
     i++; 
    } 

    return temp; 
} 

我在哪裏把我的增量像i ++,j ++,k ++和l ++?該分配只需要在項目的這一部分使用循環。我已經開始工作的項目的for循環部分。請讓我知道你的想法。這段代碼並不是真正的代碼,但我想了解如何更新這些嵌套循環的邏輯,並能夠從中獲取正確的數據。當我使用下面的變量輸出到屏幕時,我最終得到0,所以有些事情是不對的。通常我會在循環的大括號末尾加1(i ++,j ++,...等)。在這種情況下,該方法不會產生好的結果。請幫忙! :)

+0

通常,您不要將增量**放在循環的大括號的末尾**,而是將它放在花括號之前**因此它在循環中並正確更新,因此不會發生無限循環 – nem035 2014-09-22 03:24:36

+0

讓我們改變這一點有點懸而未決,因爲0仍然出現,所以我會重新編寫它。 – 2014-09-22 04:04:10

+0

當在a,b,c和d上面運行這段代碼時,它們都是零,所以近似值如此不對, – 2014-09-22 04:07:20

回答

0

與for循環相同,在循環結束時執行增量。

double[] updateArray = new double[7]; 
double w,x,y,z; 
int i=0, j=0, k=0, l=0; 
while(i < updateArray.length) 
{ 
     w = updateArray[i]; 
    while(j < updateArray.length) 
    { 
     x = updateArray[j]; 
     while(k < updateArray.length) 
     { 
      y = updateArray[k]; 
      while(l < updateArray.length) 
      { 
        z = updateArray[l]; 
       l ++; 
      } 
     k ++; 
     } 
    j ++; 
    } 
i ++; 
} 
+0

我嘗試了你在我的新編輯中看到的響應。可悲的是,當試圖圍繞該計算進行更新時,爲每個整數獲取數組的正確值似乎很困難。 for循環比這更簡單,但在這種情況下只允許while循環。 – 2014-09-22 04:13:27

0

你可以這樣做。

double[] updateArray = new double[7]; 
double w,x,y,z; 
int i=0, j=0, k=0, l=0; 
while(i++ < updateArray.length -1) 
{ 
     w = updateArray[i]; 
    while(j++ < updateArray.length -1) 
    { 
     x = updateArray[j]; 
     while(k++ < updateArray.length -1) 
     { 
      y = updateArray[k]; 
      while(l++ < updateArray.length -1) 
      { 
        z = updateArray[l]; 
      } 
     } 
    } 

} 
+2

再次看看您的建議。它會拋出ArrayIndexOutOfBoundsException。 – Radiodef 2014-09-22 03:33:25

+0

@Le達·哈'異常在線程「主」 java.lang.ArrayIndexOutOfBoundsException:7 \t在com.test.Test23.main(Test23.java:15) ' – 2014-09-22 03:37:25

1

如果你不這樣做與指數的任何魔法,只需要數組的元素在環(而不是索引),那麼你可以使用增強的for循環:

double[] updateArray = new double[7]; 
/* fill your array */ 
for(double i : updateArray) { 
    for(double w : updateArray) { 
     for(double y : updateArray) { 
      for(double x : updateArray) { 
       // No incrementing or indices required :) 
      } 
     } 
    } 
} 
+0

可悲即時通訊做計算和僅while循環被允許在這部分 – 2014-09-22 04:26:27