小白求助 为什么这个程序无法输出数组
本帖最后由 匿名 于 2021-9-13 22:20 编辑我想输入M行,N列之后输出成为数组 但是输出无法成为数组 这是为什么?
import java.util.Scanner;
public class RowColumnMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
int [][] matrix = new int;
System.out.println("Enter two numbers:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix + ",");
}
}
}
}
最后的结果想要
输入 2 3
输出
1,2,3
4,5,6 public class Test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers:");
int M = sc.nextInt();
int N = sc.nextInt();
int [][] matrix = new int;
int total = M * N;
int start = 1;
// 初始化数组
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix = start;
// 每次数字+1
start++;
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix);
// 判断是否到该行最后一个元素,如果不是,打印","
if(j < N-1)
{
System.out.print(",");
}
}
System.out.println(); // 换行
}
}
}
页:
[1]