/
digit
/
java-basics-2d-arrays
Обзор
Документация
Войти
/
digit
/
java-basics-2d-arrays
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Matrix.java
187 строк
7 KB
digit
Create: Matrix.java
29 июн 2026, 21:45
Верифицирован
29 июн 2026, 21:45
2ab9e7e
Код
Авторство
О чём код?
import java.util.Random; import java.util.Scanner; public class Matrix { // размерность матрицы public static final int SIZE = 8; public static void main(String[] args) { int[][] colors = new int[SIZE][SIZE]; int[][] rotatedColors = new int[SIZE][SIZE]; fillMatrix(colors); printMatrix(colors); rotateWithConditions(colors, rotatedColors, getRotationFromUser()); printMatrix(rotatedColors); } // метод для заполнения матрицы целыми числами от 0 до 255 public static void fillMatrix(int[][] matrix) { Random random = new Random(); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { matrix[i][j] = random.nextInt(256); } } } // метод для вывода матрицы public static void printMatrix(int[][] matrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { System.out.printf("%4d", matrix[i][j]); } System.out.println(); } System.out.println(); } // перечисление для углов поворота матрицы public enum Rotation { NO_ROTATION, ROTATE_90_CLOCKWISE, ROTATE_180, ROTATE_270_CLOCKWISE } // метод для получения выбора пользователя (нужный поворот из перечисления Rotation) public static Rotation getRotationFromUser() { Scanner sc = new Scanner(System.in); boolean inputReceived = false; // флаг для выхода из циклов // цикл для вопроса о необходимости поворота матрицы while (!inputReceived) { System.out.println("Повернуть матрицу? Y/N"); String answer = sc.nextLine(); switch (answer) { case "y": case "Y": inputReceived = true; break; case "n": case "N": return Rotation.NO_ROTATION; } } // цикл для вопроса о направлении поворота (по часовой / против часовой) boolean isClockwise = false; inputReceived = false; while (!inputReceived) { System.out.println("По часовой стрелке? Y/N"); String answer = sc.nextLine(); switch (answer) { case "y": case "Y": isClockwise = true; inputReceived = true; break; case "n": case "N": inputReceived = true; break; } } // цикл для вопроса об угле поворота int degree = 0; inputReceived = false; while (!inputReceived) { System.out.println("На 90°, 180° или 270°? 90/180/270"); String answer = sc.nextLine(); switch (answer) { case "90": degree = 90; inputReceived = true; break; case "180": degree = 180; inputReceived = true; break; case "270": degree = 270; inputReceived = true; break; } } // получение нужного поворота из перечисления (к примеру 90° против часовой -> 270° по часовой) if (isClockwise) { switch (degree) { case 90: return Rotation.ROTATE_90_CLOCKWISE; case 180: return Rotation.ROTATE_180; case 270: return Rotation.ROTATE_270_CLOCKWISE; } } else { switch (degree) { case 90: return Rotation.ROTATE_270_CLOCKWISE; case 180: return Rotation.ROTATE_180; case 270: return Rotation.ROTATE_90_CLOCKWISE; } } return Rotation.NO_ROTATION; } // метод для поворота матрицы согласно выбору пользователя (перечислению Rotation) public static void rotateWithConditions(int[][] matrix, int[][] rotatedMatrix, Rotation rotation) { switch (rotation) { case NO_ROTATION: copyMatrix(matrix, rotatedMatrix); break; case ROTATE_90_CLOCKWISE: rotateMatrix90(matrix, rotatedMatrix); break; case ROTATE_180: rotateMatrix180(matrix, rotatedMatrix); break; case ROTATE_270_CLOCKWISE: rotateMatrix270(matrix, rotatedMatrix); break; } } // метод для копирования матрицы (угол поворота отсутствует) public static void copyMatrix(int[][] matrix, int[][] notRotatedMatrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { notRotatedMatrix[i][j] = matrix[i][j]; } } } // метод для заполнения новой матрицы поворотом исходной на 90° по часовой стрелке public static void rotateMatrix90(int[][] matrix, int[][] rotatedMatrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[j][SIZE - i - 1] = matrix[i][j]; } } } // поворот на 180° по часовой стрелке public static void rotateMatrix180(int[][] matrix, int[][] rotatedMatrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[SIZE - i - 1][SIZE - j - 1] = matrix[i][j]; } } } // поворот на 270° по часовой стрелке public static void rotateMatrix270(int[][] matrix, int[][] rotatedMatrix) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { rotatedMatrix[SIZE - j - 1][i] = matrix[i][j]; } } } }