用用Java编写一个3x3矩阵,行列互换输出
时间: 2024-01-13 08:05:45 浏览: 131
输出一个矩阵java
以下是Java代码实现:
```java
public class MatrixTranspose {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// 输出原始矩阵
System.out.println("原始矩阵:");
for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
// 行列互换
int[][] transposedMatrix = new int[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}
// 输出转置后的矩阵
System.out.println("转置后的矩阵:");
for (int[] row : transposedMatrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
```
输出结果如下:
```
原始矩阵:
1 2 3
4 5 6
7 8 9
转置后的矩阵:
1 4 7
2 5 8
3 6 9
```
阅读全文