【发布时间】:2019-04-15 22:26:04
【问题描述】:
试图编写一个计算双精度数组中值的平均值的方法,但我无法得到要打印的平均值。它编译并执行,但不返回任何结果。
public class Test3Q1 {
public static void main(String[] args) {
}
public static double average (double[]x) {
double [] doubleValues = {3.0, 10.5, 19.8, 5.6, 3.2};
double total = 0.0;
for (int i =0; i<doubleValues.length; i++) {
total += doubleValues[i];
return total; }
double average = total/doubleValues.length;
System.out.println("The average of the doubles Array
is: " + average);
System.out.format("The average of the double array is:
%.1f", average);
return average;
}
}
【问题讨论】:
-
你为什么要返回循环内?
-
您也传递了
double[] x,但没有在方法中使用它。 -
而且您的
main方法也永远不会调用average。 -
你里面有一个return for sentence。您没有遍历数组并且 println 没有执行。
标签: java arrays printing average