58,454
社区成员
发帖
与我相关
我的任务
分享
public class Test3 {
public static int[][] createArray(int row) {
if (row<=0) return null;
int res[][] = new int[row][];
for (int i=0; i<row; i++) {
res[i] = new int[row-i];
/*
for (int j=0; j<res[i].length; j++) {
res[i][j] = 0;
}
*/
}
return res;
}
public static void printArray(int[][] a) {
if (a==null) {
System.out.println("a is null.");
}
for (int i=0; i<a.length; i++) {
for (int j=0; j<a[i].length; j++) {
System.out.printf("%d ", a[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = 0;
while (true) {
try {
System.out.printf("请输入行数:");
row = sc.nextInt();
if (row<=0) throw new Exception("error.");
break;
} catch(Throwable e) {
System.out.println("输入错误,请重输。");
}
}
int[][] a = createArray(row);
printArray(a);
}
}