2016-11-30 79 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