Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.3 kB
7
Indexable
Never
package array;
import java.util.Scanner;

public class Array {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("MEMBUAT HURUF F DENGAN ARRAY");
        System.out.println("============================");
        System.out.print("Masukkan panjang :");
        int x = input.nextInt();
        int y = x * 2;
        input.nextLine();
        System.out.println();
        System.out.println();
        System.out.println();
        String[][] array = new String[y][x];

        // Mengisi matriks untuk membentuk huruf "F"
        for (int i = 0; i < y; i++) {
            for (int j = 0; j < x; j++) {
                if (i == 0) {
                    array[0][j] = "*";
                } else if (i == (x - 1)) {
                    array[x - 1][j] = "*";
                } else if (j == 0) {
                    array[i][0] = "*";
                } else {
                    array[i][j] = " ";
                }
            }
        }

        // Menampilkan matriks sebelum rotasi
        System.out.println("Sebelum Rotasi:");
        displayMatrix(array);

        // Menerima inputan untuk rotasi
        System.out.println("\nTekan tombol panah untuk rotasi (kiri/kanan/atas/bawah).");
        System.out.println("Tekan 'q' untuk keluar.");

        char inputKey;
        do {
            inputKey = input.next().charAt(0);
            if (inputKey == 'q') {
                break;
            }
            array = performRotation(array, inputKey);
            displayMatrix(array);
        } while (true);
    }

    // Fungsi untuk menampilkan matriks
    private static void displayMatrix(String[][] matrix) {
        for (String[] row : matrix) {
            for (String value : row) {
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }

    // Fungsi untuk rotasi matriks berdasarkan input tombol panah
    private static String[][] performRotation(String[][] matrix, char direction) {
        switch (direction) {
            case 'L':
                return rotateLeft(matrix);
            case 'R':
                return rotateRight(matrix);
            case 'U':
                return rotateUp(matrix);
            case 'D':
                return rotateDown(matrix);
            default:
                System.out.println("Input tidak valid.");
                return matrix;
        }
    }

    // Fungsi untuk rotasi ke kiri
    private static String[][] rotateLeft(String[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        String[][] rotatedMatrix = new String[cols][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotatedMatrix[cols - 1 - j][i] = matrix[i][j];
            }
        }

        return rotatedMatrix;
    }

    // Fungsi untuk rotasi ke kanan
    private static String[][] rotateRight(String[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        String[][] rotatedMatrix = new String[cols][rows];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotatedMatrix[j][rows - 1 - i] = matrix[i][j];
            }
        }

        return rotatedMatrix;
    }

    // Fungsi untuk rotasi ke atas
    private static String[][] rotateUp(String[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        String[][] rotatedMatrix = new String[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotatedMatrix[i][j] = matrix[i][j];
            }
        }

        return rotatedMatrix;
    }

    // Fungsi untuk rotasi ke bawah
    private static String[][] rotateDown(String[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;

        String[][] rotatedMatrix = new String[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rotatedMatrix[rows - 1 - i][j] = matrix[i][j];
            }
        }

        return rotatedMatrix;
    }
}
Leave a Comment