2016-11-30 2 views
-2

Für eine meiner Zuweisungen muss ich den ursprünglichen Code ändern, um mit einer Zeichenfolge zu arbeiten, die an eine andere Funktion übergeben wird, also konvertiere ich diese für Schleife in eine while-Schleife, aber ich kann es nicht richtig verstehen. Wo gehe ich falsch?Wie man Java für Schleife zu while Schleife ändert

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; 

} 

Mein Versuch:

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; 

    } 
} 

Auch hatte ich die Halter von einem int in einen String zu konvertieren, weil wir es konvertieren.

+1

Nicht mein downvote, aber warum brauchen Sie von einem 'for' zu' while' Schleife in erster Linie zu konvertieren? –

+0

'für (int i = 0; i <7; i ++)' ist vielleicht das gleiche wie 'while (i ++ <7)' –

+0

auch in Ihrem Code 'x' wird nie inkrementiert –

Antwort

0

versuchen diese ...

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

Danke! Das hat sehr geholfen! – DavidA