java随机产生一个3*3数组矩阵,行列互换输出
时间: 2023-04-06 18:03:28 浏览: 78
可以回答这个问题。以下是Java代码实现:
```java
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[][] matrix = new int[3][3];
Random random = new Random();
// 随机生成矩阵
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = random.nextInt(10);
}
}
System.out.println("原始矩阵:");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
// 行列互换
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
System.out.println("行列互换后的矩阵:");
for (int[] row : matrix) {
System.out.println(Arrays.toString(row));
}
}
}
```
输出结果:
```
原始矩阵:
[6, 9, 9]
[2, 2, 8]
[8, 7, 6]
行列互换后的矩阵:
[6, 2, 8]
[9, 2, 7]
[9, 8, 6]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)