Java小白求助,Java基础题目,恳请各位高手帮忙

qq_41722962 2020-05-15 08:08:35
题目示例:(不是问题)


欢迎感兴趣的程序大佬在帖子下面留言,将不定期放送Java题目供大佬们挑战
...全文
142 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱吃鱼的ねこ 2020-05-16
  • 打赏
  • 举报
回复
引用 2 楼 qq_41722962的回复:
[quote=引用 1 楼 qybao 的回复:] 随手写一个吧

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);
    }
}
,希望大佬可以持续关注本贴,后续会放送更多题目[/quote] 第一次见到把白嫖说的这么好听的😂
qybao 2020-05-15
  • 打赏
  • 举报
回复
随手写一个吧

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

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧