public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// N阶方阵int n = scanner.nextInt();// 构建方阵List<List<Integer>> matrix = new ArrayList<>();for (int i = 0; i < n; i++) {List<Integer> row = new ArrayList<>();for (int j = 0; j < n; j++) {row.add(i * n + j + 1);}matrix.add(row);}// 打印方阵数据print(matrix);// 旋转M次int m = scanner.nextInt();// 旋转方阵matrix = rotate(matrix, m % 4); // 旋转4次等于不旋转// 打印旋转后的方阵print(matrix);}static void print(List<List<Integer>> matrix) {for (List<Integer> row : matrix) {for (Integer integer : row) {System.out.print(integer + " ");}System.out.println();}}static List<List<Integer>> rotate(List<List<Integer>> matrix, int m) {// todo 参数校验异常处理if(m == 0){return matrix;}List<List<Integer>> result = new ArrayList<>();int size = matrix.size();// 旋转方阵90度for (int i = 0; i < size; i++) {List<Integer> row = new ArrayList<>();for (int j = 0; j < size; j++) {row.add(matrix.get(size - j - 1).get(i));}result.add(row);}return rotate(result, m - 1);}
运行