用java编写程序,在main方法中定义一个大小为4*4的二维整型数组,数组元素如下所示: 123 345 32 89 213 206 85 77 49 411 114 220 903 227 319 166 (1)分别输出数组中元素最大值和最小值的下标。 (2)计算每个元素值的各个位数之和,将和作为新元素,生成一个新的4*4的二维整型数组并按题目示例输出
时间: 2023-12-10 11:37:15 浏览: 102
以下是Java代码实现:
```java
public class ArrayExample {
public static void main(String[] args) {
int[][] array = {{123, 345, 32, 89}, {213, 206, 85, 77}, {49, 411, 114, 220}, {903, 227, 319, 166}};
int max = array[0][0];
int min = array[0][0];
int maxRow = 0;
int maxCol = 0;
int minRow = 0;
int minCol = 0;
// 找出数组中的最大值和最小值及其下标
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] > max) {
max = array[i][j];
maxRow = i;
maxCol = j;
}
if (array[i][j] < min) {
min = array[i][j];
minRow = i;
minCol = j;
}
}
}
System.out.println("最大值为" + max + ",下标为[" + maxRow + "][" + maxCol + "]");
System.out.println("最小值为" + min + ",下标为[" + minRow + "][" + minCol + "]");
// 计算每个元素值的各个位数之和,生成新数组并输出
int[][] newArray = new int[4][4];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
int sum = 0;
int num = array[i][j];
while (num != 0) {
sum += num % 10;
num /= 10;
}
newArray[i][j] = sum;
System.out.print(newArray[i][j] + "\t");
}
System.out.println();
}
}
}
```
输出结果如下:
```
最大值为903,下标为[3][0]
最小值为32,下标为[0][2]
6 12 5 17
6 8 13 14
13 6 6 4
12 11 13 13
```
阅读全文