java一个二维数组,有10个元素,打印出低于数组元素平均值的元素
时间: 2024-05-08 15:17:00 浏览: 79
java代码-定义一个一维数组,求出数组的最大值,最小值,平均值
以下是Java代码实现:
```
public class Main {
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}, {13, 14, 15}, {16, 17, 18}, {19, 20, 21}, {22, 23, 24}, {25, 26, 27}, {28, 29, 30}};
double sum = 0;
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
count++;
}
}
double avg = sum / count;
System.out.println("平均值为:" + avg);
System.out.println("低于平均值的元素为:");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] < avg) {
System.out.print(arr[i][j] + " ");
}
}
}
}
}
```
输出结果为:
```
平均值为:15.5
低于平均值的元素为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
```
阅读全文