如何将 java for 循环更改为 while 循环

How to change java for loop to while loop

对于我的一项作业,我需要修改原始代码以处理将传递给另一个函数的字符串,因此我将此 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;

    }
}

此外,我必须将 holder 从 int 转换为 String,因为我们必须对其进行转换。

试试这个...

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;
}