/
TrenikhinKirill
/
MatrixRotation
Обзор
Документация
Войти
/
TrenikhinKirill
/
MatrixRotation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/Main.java
70 строк
2 KB
TrenikhinKirill
first_commit
30 июл 2026, 12:42
30 июл 2026, 12:42
37fdb5b
Код
Авторство
О чём код?
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]; Random random = new Random(); // Заполнение случайными числами от 0 до 255 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { colors[i][j] = random.nextInt(256); } } System.out.println("Исходная матрица:"); printMatrix(colors); // Запрос угла поворота Scanner scanner = new Scanner(System.in); int angle = 0; while (true) { System.out.print("Введите угол поворота (90, 180 или 270): "); if (scanner.hasNextInt()) { angle = scanner.nextInt(); if (angle == 90 || angle == 180 || angle == 270) { break; } } System.out.println("Некорректный ввод. Пожалуйста, введите 90, 180 или 270."); scanner.nextLine(); } scanner.close(); // Поворот матрицы на заданный угол int[][] rotated = colors; int times = angle / 90; for (int i = 0; i < times; i++) { rotated = rotateClockwise(rotated); } System.out.println("\nМатрица после поворота на " + angle + "°:"); printMatrix(rotated); } // Вывод матрицы с форматированием public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.format("%4d", matrix[i][j]); } System.out.println(); } } // Поворот квадратной матрицы на 90° по часовой стрелке public static int[][] rotateClockwise(int[][] source) { int n = source.length; int[][] result = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { result[j][n - 1 - i] = source[i][j]; } } return result; } }