显示转置方法时出现问题?

Problems displaying a transposed method?

我正在尝试编写一个程序来打印随机生成的矩阵和转置矩阵,但我不太明白。 当我尝试编译时,第 41 行出现错误“错误:找不到符号 printMatrix(transposedMatrix); ^ 我已经为此工作了几个小时,但无法弄清楚,在此先感谢您的帮助。

如果这段代码的格式有点不对,我深表歉意,它在 Sublime 中看起来不错,我不习惯这个网站。

import java.util.Scanner;

public class Matrix {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

int rows = 0;
int cols = 0;

while (rows < 1 || rows > 10) {
System.out.print("Enter the number of rows (1-10): ");
int userRows = input.nextInt();

if (userRows < 1 || userRows > 10) {
  System.out.println("ERROR! The number of rows cannot be outside the specified range of 1-10!");
}
else userRows += rows;
}

while (cols < 1 || cols > 10) {
System.out.print("Enter the number of columns (1-10): ");
int userCols = input.nextInt();

if (userCols < 1 || userCols > 10) {
  System.out.println("ERROR! The number of columns cannot be outside the speified range of 1-10!");
}
else userCols += cols;
}

int[][] originalMatrix = new int[rows][cols];

for (int row = 0; row < originalMatrix.length; row++)
  for (int col = 0; col < originalMatrix[row].length; col++) {
    originalMatrix[row][col] = (int) (Math.random() * 1000);
  }

System.out.println("\nOriginal matrix:");
printMatrix(originalMatrix);

System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix);
}

public static void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
  for (int col = 0; col < matrix[row].length; col++) {
    System.out.print(matrix[row][col] + "  ");
  }
  System.out.println();
} 
} 

public static int[][] transposedMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] transposedMatrix = new int[n][m];
for(int x = 0; x < n; x++) {
    for(int y = 0; y < m; y++) {
        transposedMatrix[x][y] = matrix[y][x];
    }
}
return transposedMatrix;
}
}

更改打印语句以调用您创建的方法。确保也传入 originalMatrix

printMatrix(transposedMatrix(originalMatrix));

也是题外话,但您的代码中存在内存泄漏。使用后永远不要关闭 Scanner。最好在使用完扫描仪后将其关闭。将此添加到 main 方法的末尾以阻止内存泄漏。

input.close()

System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix);

这应该是

System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix(originalMatrix));