如题所述
以下是Python代码实现:
arr = [1.2, 3.4, 5.6, 7.8, 9.0, 10.1] # 输入数组
sum = 0.0 # 初始化总和为0
# 遍历数组,累加每个元素
for num in arr:
sum += num
# 计算平均值
avg = sum / len(arr)print("平均值为:", avg)
以下是Java代码实现:
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
double[] arr = new double[6];
double sum = 0; for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextDouble();
sum += arr[i];
}
double average = sum / arr.length; System.out.printf("平均值为:%.2f", average);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2023-03-07
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 定义一个有六个元素的浮点数组
double[] array = new double[6]; // 输入六个元素的值
System.out.println("请输入六个元素的值:"); for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextDouble();
} // 计算平均值
double sum = 0; for (int i = 0; i < array.length; i++) {
sum += array[i];
} double average = sum / array.length; // 输出结果
System.out.println("平均值为:" + average);
}
}
double[] array = new double[6]; // 输入六个元素的值
System.out.println("请输入六个元素的值:"); for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextDouble();
} // 计算平均值
double sum = 0; for (int i = 0; i < array.length; i++) {
sum += array[i];
} double average = sum / array.length; // 输出结果
System.out.println("平均值为:" + average);
}
}
第2个回答 2023-03-06
参考代码如下: