簡體   English   中英

java 二維數組/矩陣必須隨用戶輸入旋轉

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

我在使用用戶輸入創建矩陣並將其旋轉 90 度時遇到問題。 任何人都知道為什么矩陣的范圍有問題?

問題是當輸入元素時它只顯示 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();
        }
    }
}

如果您使用 *m 矩陣(非方矩陣),您將無法工作。 我們必須使用額外的空間

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];
        }   
    }

我希望這有助於回答您的問題。

當你說“旋轉”你的矩陣時,我認為你想得太多了。

我剛剛用用戶輸入做了一個例子,它看起來不錯,如果這對你有幫助,請告訴我:

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);

    }

首先你要求行數,然后是列數。

其次,您必須在每個單元格中詢問值(在第一個循環中我就是這樣做的)。

第三,我用打印方法打印矩陣的內容。

第四,我將矩陣發送到旋轉方法,在旋轉方法的主體中,我發送了第二個矩陣進行打印。

最后,關閉掃描儀。

我用下一個數據測試它:

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

它向我展示了下一個轉換:

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

我希望這是想要的結果。

干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM