无法得到标准偏差,我做错了什么?

use*_*605 2 java math

我基本上试图使用一个数组,即从文件中读取的数据,然后使用该数组计算数据的均值和标准差.

我似乎无法得到正确的号码.

static public double[][] calcStats(String[][] x) throws IOException {
  double[][] array = new double[7][2];
  double total = 0, std_dev = 0, amount = 0;
  int row2 = 0;
  for (int row = 1; row < x.length; row++) {
    array[row2][0] = (total);
    array[row2][1] = Math.sqrt(amount);
    amount = 0;
    total = 0;
    if (row >= 2) {
      row2++;
    }
    for (int col = 1; col < x[row].length; col++) {
      total += Integer.parseInt(x[row][col]);
      if (col == 4) {
        total = (total / 4);
      }
    }
    for (int col = 1; col < x[row].length; col++) {
      std_dev = (Integer.parseInt(x[row][col])) - (total);
      std_dev = Math.pow(std_dev, 2);
      amount = +std_dev;
      std_dev = 0;
      if (col == 4) {
        amount = (amount / 27);
      }
    }
  }
  array[row2][0] = (total);
  return array;
}
Run Code Online (Sandbox Code Playgroud)

Ted*_*opp 5

Java中的数组从0开始.您在1处开始循环.这意味着您缺少每个数组的第一个元素.

根据Marko Topolnik的建议,我应该指出我amount =+ std_dev;已将原始代码更改为amount += std_dev;.现在我考虑一下,这是一个无效的编辑,因为原始代码是一个额外的问题(除了循环限制).我把编辑推回到Marco的版本.