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

Java 显示转置方法时出现问题?,java,arrays,matrix,transpose,Java,Arrays,Matrix,Transpose,我试图写一个程序,打印一个随机生成的矩阵和一个转置矩阵,但我不太明白。 当我试图编译时,我在第41行得到错误错误:找不到符号printMatrixtransposedMatrix; ^ 这件事我已经做了几个小时了,还没弄明白,提前谢谢你的帮助 我很抱歉,如果这段代码的格式有点不合适,它看起来很好在崇高,我不习惯这个网站 import java.util.Scanner; public class Matrix { public static void main(String[] args)

我试图写一个程序,打印一个随机生成的矩阵和一个转置矩阵,但我不太明白。 当我试图编译时,我在第41行得到错误错误:找不到符号printMatrixtransposedMatrix; ^ 这件事我已经做了几个小时了,还没弄明白,提前谢谢你的帮助

我很抱歉,如果这段代码的格式有点不合适,它看起来很好在崇高,我不习惯这个网站

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

更改print语句以调用创建的方法。确保也通过了原始矩阵

打印矩阵TransposedMatrix原始矩阵

也离题了,但代码中存在内存泄漏。使用扫描仪后,您永远不会关闭它。最好在使用完扫描仪后关闭它。将此添加到主方法的末尾,以停止内存泄漏

输入。关闭

这应该是

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

transposedMatrix是方法的名称,也是它的局部变量,请尝试将变量重命名为其他名称。您的标题与您遇到的问题关系不大。请更改它以反映实际问题。仅供参考,已有许多矩阵包;一年前我用过一个NASA,非常好,非常好,谢谢!我刚开始java编程几周,还没有遇到input.close。从现在起我一定要用它,再次谢谢。
System.out.println("\nTransposed matrix:");
printMatrix(transposedMatrix(originalMatrix));