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

时间:2016-11-30 02:42:07

标签: java for-loop while-loop converter

对于我的一个作业,我需要修改原始代码以使用将传递给另一个函数的字符串,以便我将此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转换为String,因为我们必须转换它。

1 个答案:

答案 0 :(得分: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;
}