Java-comment faire pivoter le tableau carré (bidimensionnel) de 90 degrés vers la droite?


Alors, comment cela devrait-il fonctionner. J'entre "n" et " m " pour créer une matrice bidimensionnelle avec une résolution n*m. J'ai besoin de le faire pivoter de 90 degrés vers la droite pour le rendre en quelque sorte comme cette image faite dans paint:

entrez la description de l'image ici

J'ai écrit du code mais je ne peux pas vraiment le faire fonctionner - on dirait que c'est facile mais chaque fois que j'essaie de le faire fonctionner, j'obtiens "outofboundsexception". Le voici:

 import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        int n = s1.nextInt();
        int m = s1.nextInt();
        System.out.println();
        int[][] array = new int[n][m];
        int[][] ar = new int[n][m];
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[n - 1].length; j++) {
                array[i][j] = s1.nextInt();
            }
        }
        System.out.println("INPUT ARRAY :");
        System.out.println();
        for(int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[n - 1].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        /* here is the main actions with the array begin*/
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[n - 1].length; j++) {
                ar[i][j]=array[n - j - 1][i];
            }
        }
        /*the end of actions with the array*/
        System.out.println("TASK ARRAY :");
        System.out.println();
        for(int i = 0; i < ar.length; i++) {
            for (int j = 0; j < rr[n - 1].length; j++) {
                System.out.print(ar[i][j] + " ");
            }
            System.out.println();
        }
    }


}

Qu'est-ce que je fais exactement de mal?

Author: snow4dv, 2018-10-01

2 answers

Voici la version correcte de votre programme, avec tous les indices for-loop fixes, et l'impression déplacée dans une méthode distincte.

public class Main {
    static void print(String title, int[][] array) {
        System.out.println(title);
        System.out.println();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        int m = s1.nextInt();
        int n = s1.nextInt();
        System.out.println();
        int[][] array = new int[m][n];
        int[][] ar = new int[n][m];
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++) {
                array[i][j] = s1.nextInt();
            }
        }

        print("INPUT ARRAY :", array);

        /* here is the main actions with the array begin*/
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++) {
                ar[j][m - i - 1] = array[i][j];
            }
        }

        /*the end of actions with the array*/
        print("TASK ARRAY :", ar);
    }
}
 -1
Author: Leo Aso, 2018-10-01 20:13:43

`double x1 = point.x - center.x; double y1 = point.y - center.y;

Double x2 = x1 * Math.cos (angle) - y1 * Math.sin(angle)); double y2 = x1 * Math.sin ( angle) + y1 * Math.cos (angle));

Point.x = x2 + centre.x; point.y = y2 + centre.y` '

Qui devrait tranformer chaque point, il devrait être assez simple à partir de là. Le centre est la longueur/2

 -3
Author: Devan S., 2018-10-01 19:53:48