我知道这是一个问题,但是我似乎找不到用户输入的行数所发生的错误。当我输入数字或行时,它会完全忽略该命令。只有列对行数和列数都有效。
import java.util.Scanner;
public class DisplayMatrix
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int n = input.nextInt();
System.out.print("Enter the number of columns: ");
int m = input.nextInt();
printMat(m);
printMatrix(n);
}
public static void printMatrix(int n) {
for (int rows = 0; rows < n; rows++) {
System.out.println();
System.out.print((int)(Math.random() * 2));
}
}
public static void printMat(int m) {
for (int cols = 0; cols < m; cols ++) {
System.out.print((int)(Math.random() * 2));
}
}
}
发布于 2021-03-09 05:57:29
对于打印矩阵,您可以使用如下循环:
for(int rows=0;rows<n;rows++){
for(int col=0;col<m;col++){
System.out.print((int)(Math.random() * 2)+ " ");
}
System.out.println();
}
https://stackoverflow.com/questions/66541448
复制相似问题