/
Kmaxim
/
MatrixRotation
Обзор
Документация
Войти
/
Kmaxim
/
MatrixRotation
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
MatrixRotation.java
92 строки
3 KB
Kmaxim
upload files
19 фев 2025, 20:32
19 фев 2025, 20:32
38b4b42
Код
Авторство
О чём код?
//TIP Для <b>запуска</b> кода нажмите <shortcut actionId="Run"/> или // щелкните значок <icon src="AllIcons.Actions.Execute"/> в боковой области. import java.util.Random; import java.util.Scanner; public class MatrixRotation { public static final int SIZE = 8; public static void main(String[] args) { int[][] colors = new int[SIZE][SIZE]; Random random = new Random(); Scanner scanner = new Scanner(System.in); 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); System.out.println("\nВыберите угол поворота:"); System.out.println("1. 90 градусов"); System.out.println("2. 180 градусов"); System.out.println("3. 270 градусов"); System.out.print("Ваш выбор: "); int choice = scanner.nextInt(); int[][] rotatedColors = null; switch (choice) { case 1: rotatedColors = rotateMatrix90(colors); System.out.println("\nМатрица после поворота на 90 градусов:"); break; case 2: rotatedColors = rotateMatrix180(colors); System.out.println("\nМатрица после поворота на 180 градусов:"); break; case 3: rotatedColors = rotateMatrix270(colors); System.out.println("\nМатрица после поворота на 270 градусов:"); break; default: System.out.println("Неверный выбор. Программа завершена."); return; // Завершаем программу при неверном выборе } printMatrix(rotatedColors); } public static int[][] rotateMatrix90(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[][] rotateMatrix180(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[][] rotateMatrix270(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; } public static void printMatrix(int[][] matrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { System.out.format("%4d", matrix[i][j]); } System.out.println(); } } }