/
spat
/
RotateMatrix
Обзор
Документация
Войти
/
spat
/
RotateMatrix
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
84 строки
3 KB
spat
upload files
14 фев 2026, 02:52
Верифицирован
14 фев 2026, 02:52
049e4ef
Код
Авторство
О чём код?
import java.util.Random; import java.util.Scanner; public class Main { public static final int SIZE = 8; public static void main(String[] args) { int[][] colors = new int[SIZE][SIZE]; int[][] rotatedColors = new int[SIZE][SIZE]; Random random = new Random(); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { // для случайных значений воспользуемся готовым решением из библиотеки java.util.Random colors[i][j] = random.nextInt(256); } } showMatrix(colors); Scanner sc = new Scanner(System.in); System.out.println("Введите 1, если хотите повернуть матрицу на 90 градусов, 2 - на 180 градусов, 3 - на 270 градусов"); int input = sc.nextInt(); switch (input) { case 1: rotatedColors = rotateMatrix90Degrees(colors); break; case 2: rotatedColors = rotateMatrix180Degrees(colors); break; case 3: rotatedColors = rotateMatrix270Degrees(colors); default: break; } System.out.println("Результат:"); showMatrix(rotatedColors); } public static void showMatrix(int[][] matrix) { for (int[] row : matrix) { for (int cell : row) { // %4d означает, что мы под каждый номер резервируем 4 знака // (незанятые будут заполнены пробелами) // таким образом, у нас получится ровная таблица System.out.format("%4d", cell); } // Переход на следующую строку System.out.println(" "); } } public static int[][] rotateMatrix90Degrees(int[][] matrix) { int[][] rotatedMatrix = new int[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[j][SIZE - 1 - i] = matrix[i][j]; } } return rotatedMatrix; } public static int[][] rotateMatrix180Degrees(int[][] matrix) { int[][] rotatedMatrix = new int[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[SIZE - 1 - i][SIZE - 1 - j] = matrix[i][j]; } } return rotatedMatrix; } public static int[][] rotateMatrix270Degrees(int[][] matrix) { int[][] rotatedMatrix = new int[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[SIZE - 1 - j][i] = matrix[i][j]; } } return rotatedMatrix; } }