我正在尝试计算出数组的平均值,但我无法将int转换为double?为什么会这样?
private void AvgOfArray(){
myWindow.clearOut();
int total = 0;
int[] a = new int[4];
int i = 0;
double avg = 0.0;
while (i < a.length) {
a[i] = 1 + (int) (Math.random() * 10);
myWindow.writeOutLine(a[i]);
total += a[i];
i++;
}
avg = (Double) i;
myWindow.writeOutLine(total/Double.parseDouble(i));
}
答案 0 :(得分:1)
计算平均值的正确方法是,
double avg = total / (double) i;
myWindow.writeOutLine(avg);
将int
装箱到Double
没有多大意义,尤其是尝试解析它。