简体   繁体   English

java 二维数组/矩阵必须随用户输入旋转

[英]java 2d array/matrix got to rotate with user input

I have a problem with creating a matrix with user input and rotate it 90 degrees.我在使用用户输入创建矩阵并将其旋转 90 度时遇到问题。 Anyone knows why there is a problem with the range of the matrix?任何人都知道为什么矩阵的范围有问题?

The problem is when the elements are entered and it only shows up like 2 or 3 elements even if i write that I want 16..问题是当输入元素时它只显示 2 或 3 个元素,即使我写我想要 16..

public static void main(String args[]) {
        int m, n, i, j;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
//taking row as input
        m = sc.nextInt();
        System.out.print("Enter the number of columns: ");
//taking column as input
        n = sc.nextInt();
// Declaring the two-dimensional matrix
        int[][] array = new int[m][n];
// Read the matrix values
        System.out.println("Enter the elements of the array: ");
//loop for row
        for (i = 0; i < m; i++)
//inner for loop for column
            for (j = 0; j < n; j++)
                array[i][j] = sc.nextInt();
//accessing array elements
        System.out.println("Elements of the array are: ");
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++)
//prints the array elements
                System.out.print(array[i][j] + " ");
//throws the cursor to the next line
            System.out.println();
            System.out.println("The matrix after being rotated 90 degrees clockwise: ");
            rotate(array);
        }
    }


   static int M = 4;

    // Method to rotate the matrix 90 degree clockwise
    static void rotate(int[][] arr) {
        // printing the matrix on the basis of the index
        for (int j = 0; j < M; j++) {
            for (int i = M - 4; i >= 0; i--)

                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
}

Your wont work if you an*m matrix(non square matrix).如果您使用 *m 矩阵(非方矩阵),您将无法工作。 We have to use an extra space我们必须使用额外的空间

public void rotate(int[][] matrix) {
    
    int A[][] = new int[matrix.length][matrix[0].length];
    for(int i=0; i<matrix.length; i++){
        for(int j=0; j<matrix[i].length; j++){
            A[i][j] = matrix[j][i];
        }   
    }
    
    for(int i=0; i<matrix.length; i++){
        for(int j=0; j<matrix[i].length; j++){
            matrix[i][j] = A[i][A[i].length-1-j];
        }   
    }

I hope this helps to answer your questions.我希望这有助于回答您的问题。

I think that you are overthinking when you say "rotate" your matrix.当你说“旋转”你的矩阵时,我认为你想得太多了。

I just made an example with user input and it does seem Ok, let me know if this helps you:我刚刚用用户输入做了一个例子,它看起来不错,如果这对你有帮助,请告诉我:

public void test() {

        final Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the matrix height (number of rows):");

        int height = scanner.nextInt();

        System.out.println("Enter the matrix width  (number of columns):");

        int width = scanner.nextInt();
        
        int[][] matrix = new int[height][width];

        System.out.println("Now, enter the matrix content.");

        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                System.out.print(String.format("Enter the value to cell in the point %d, %d", i, j));

                matrix[i][j] = scanner.nextInt();

            }

            System.out.println();

        }

        print(matrix);

        
        scanner.close();
    }

    void print(final int[][] array) {
        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();
        }
    }



   void rotate(int[][] array) {
        int[][] other = new int[array[0].length][array.length];

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {

                other[j][i] = array[i][j];
            }
        }

        print(other);

    }

First you ask for the number of rows, then columns.首先你要求行数,然后是列数。

Second, you have to ask for values in each cell (in the firt loop is what i do).其次,您必须在每个单元格中询问值(在第一个循环中我就是这样做的)。

Third, i print the content of the matrix with the print method.第三,我用打印方法打印矩阵的内容。

Fourth, i send the matrix to a rotate method, in the body of the rotate method, i sent the second matrix to print.第四,我将矩阵发送到旋转方法,在旋转方法的主体中,我发送了第二个矩阵进行打印。

Finally, close the scanner.最后,关闭扫描仪。

I test it with the next data:我用下一个数据测试它:

1,2,3,4
5,6,7,8
9,10,11,12,

It showed me the next conversion:它向我展示了下一个转换:

1,5,9
2,6,10
3,7,11
4,8,12

I hope this is the desired result.我希望这是想要的结果。

Cheers.干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM