检查以下代码的错误import java.util.Scanner; public class V9 { public static void main(String[] args) { int[] intArray = getValues(10); double[] doubleArray = getValues(10); double intAvg = average(intArray); System.out.println("这10个整数的平均数为:" + intAvg); double doubleAvg = average(doubleArray); System.out.println("这10个double型值的平均数为:" + doubleAvg); } public static int[] getValues(int size) { Scanner scanner = new Scanner(System.in); int[] array = new int[size]; System.out.println("请输入" + size + "个整数:"); for (int i = 0; i < array.length; i++) { array[i] = scanner.nextInt(); } return array; } public static <T extends Number> double average(T[] array) { double sum = 0.0; for (T t : array) { sum += t.doubleValue(); } return sum / array.length; } }
时间: 2023-06-03 08:03:36 浏览: 137
这个代码的错误是缺少了加号,正确的代码如下:
import java.util.Scanner;
public class V9 {
public static void main(String[] args) {
int[] intArray = getValues(10);
double[] doubleArray = getValues(10);
double intAvg = average(intArray);
System.out.println("这10个整数的平均数为:" + intAvg);
double doubleAvg = average(doubleArray);
System.out.println("这10个double型值的平均数为:" + doubleAvg);
}
public static int[] getValues(int size) {
Scanner scanner = new Scanner(System.in);
int[] array = new int[size];
System.out.println("请输入" + size + "个整数:");
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
return array;
}
public static <T extends Number> double average(T[] array) {
double sum = 0.0;
for (T t : array) {
sum += t.doubleValue();
}
return sum / array.length;
}
}
阅读全文