java利用随机数产生一个10行,10列的整型矩阵.完成如下操作(1)输出矩阵中元素最大值及最大值所在位置(2)输出该矩阵的转置矩阵
时间: 2023-09-17 17:13:06 浏览: 233
JAVA实现矩阵的加减和转置
下面是Java代码实现:
```java
import java.util.Random;
public class Matrix {
public static void main(String[] args) {
int[][] matrix = new int[10][10];
Random random = new Random();
// 随机生成矩阵
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
matrix[i][j] = random.nextInt(100);
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// 找到最大值及其位置
int max = matrix[0][0];
int row = 0;
int col = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
row = i;
col = j;
}
}
}
System.out.println("最大值为:" + max + ",位置为:第" + (row + 1) + "行,第" + (col + 1) + "列");
// 转置矩阵
int[][] transposedMatrix = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
transposedMatrix[i][j] = matrix[j][i];
}
}
// 输出转置矩阵
System.out.println("转置矩阵为:");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
}
}
```
运行结果如下:
```
50 73 1 12 95 86 28 81 46 39
12 67 57 53 48 16 77 70 64 9
98 88 64 92 73 91 80 17 24 78
29 4 92 5 43 18 35 16 51 49
89 15 11 61 54 87 88 58 98 30
42 87 23 49 31 78 95 92 56 56
71 56 74 94 99 70 69 85 8 44
45 98 27 91 82 45 68 47 91 45
78 94 70 27 4 93 3 8 83 49
0 6 16 63 96 9 52 41 69 2
最大值为:99,位置为:第7行,第5列
转置矩阵为:
50 12 98 29 89 42 71 45 78 0
73 67 88 4 15 87 56 98 94 6
1 57 64 92 11 23 74 27 70 16
12 53 92 5 61 49 94 91 27 63
95 48 73 43 54 31 99 82 4 96
86 16 91 18 87 78 70 45 93 9
28 77 80 35 88 95 69 68 3 52
81 70 17 16 58 92 85 47 8 41
46 64 24 51 98 56 8 91 83 69
39 9 78 49 30 56 44 45 49 2
```
阅读全文