2016-11-30 78 views
-2

對於我的任務之一,我需要修改原始代碼以使用將傳遞給另一個函數的字符串,以便將此循環轉換爲while循環,但我似乎無法正確理解。我哪裏錯了?如何將java for循環更改爲while循環

public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) { 

    double small = errorCalculation[0]; 
    computedRatios = new double[length]; 
    int holder = 0; 
    //int x = 0; 
    for (int i =0; i<7; i++) 
     { 
      if(errorCalculation[i] < small) 
      { 
       small = errorCalculation[i]; 
       holder = i; 
      } 
     } 


    computedError = small; 
    computedRatios = new double[length]; 

    for (int x = 0; x < length; x++) { 
     computedRatios[x] = ratios[holder][x]; 
    } 
    //String str1 = String.valueOf(holder); 

    // bigODeter = str1; 
    bigODeter = holder; 

} 

我嘗試:

public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) { 

    double small = errorCalculation[0]; 
    computedRatios = new double[length]; 
    int holder = 0; 
    int x = 0; 

    /*for (int x = 0; x < 7; x++) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 
     } 
    }*/ 
    while (x == 0) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
     } 
     holder = x; 
    } 

    while (x == 1) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 
    } 

    while (x == 2) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 
    } 

    while (x == 3) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 
    } 

    while (x == 4) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 
    } 

    while (x == 5) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 
    } 

    while (x == 6) { 
     if (errorCalculation[x] < small) { 
      small = errorCalculation[x]; 
      holder = x; 

     } 

     computedError = small; 
     computedRatios = new double[length]; 

     for (x = 0; x < length; x++) { 
      computedRatios[x] = ratios[holder][x]; 
     } 
     String str1 = String.valueOf(holder); 

     bigODeter = str1; 

    } 
} 

此外,我不得不持有人從一個int轉換爲字符串,因爲我們必須把它轉換。

+1

不是我的downvote,但是你爲什麼需要從'for'到'while'循環轉換擺在首位? –

+0

'for(int i = 0; i <7; i ++)'也許和'while(i ++ <7)'相同' –

+0

也在你的代碼中'x'永遠不會增加 –

回答

0

試試這個...

public void smallestError(double[] errorCalculation, double[] mean, double[][] ratios, int length) { 

double small = errorCalculation[0]; 
computedRatios = new double[length]; 
int holder = 0; 
//int x = 0; 
    int i =0; 
    while (i<7) 
    { 
     if(errorCalculation[i] < small) 
     { 
      small = errorCalculation[i]; 
      holder = i; 
     } 
    i++; 
    } 


computedError = small; 
computedRatios = new double[length]; 

int x= 0; 

while (x < length){ 

     computedRatios[x] = ratios[holder][x]; 
     x++; 

} 
//String str1 = String.valueOf(holder); 
// bigODeter = str1; 
    bigODeter = holder; 
}  
+0

謝謝!這非常有幫助! – DavidA