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[M][N];
int total = M * N;
int start = 1;
// 初始化数组
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = start;
// 每次数字+1
start++;
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix[i][j]);
// 判断是否到该行最后一个元素,如果不是,打印","
if(j < N-1)
{
System.out.print(",");
}
}
System.out.println(); // 换行
}
}
}
|