import java.util.Random;
import java.util.Scanner;
public class MyText {
public static void main(String[] args) {
System.out.printf("请输入行和列:");
Scanner scan = new Scanner(System.in);//以空格作为分隔符
int m= scan.nextInt();
int n = scan.nextInt();
scan.close();//关闭扫描器
int[][] arr = new int[m][n];
Random r = new Random();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = r.nextInt(100); //给每个数组元素赋值
}
}
for (int a = 0; a< m; a++) {
for (int j = 0; j < n; j++) {
System.out.printf("%d ", arr[a][j]);
}
System.out.printf("\n");
}
System.out.printf("转置后:\n");
int[][] newArr = new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) {
newArr[i][j] = arr[j][i];
}
}
for (int a = 0; a< n; a++) {
for (int j = 0; j < m; j++) {
System.out.printf("%d ", newArr[a][j]);
}
System.out.printf("\n");
}
}
}
12-11
877
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)