创建一个类,要求用户输入数字,然后根据 int 输入打印出以下模式。
所以我制作的代码结果看起来像这样......
12345
1234
123
12
但它应该看起来像这样
5
45
345
2345
12345
Scanner tri = new Scanner(System.in);
System.out.println("Enter a postive integer.");
int shape = tri.nextInt();
for(int c = shape; c > 1; --c){
for (int a = 1; a <= shape-c; a++){
System.out.print(" ");
}
for(int d = 1; d <= c; d++){
System.out.print(d);
}
System.out.println(" ");
最佳答案
你能试试下面的代码吗?
Scanner tri = new Scanner(System.in);
System.out.println("Enter a postive integer.");
int shape = tri.nextInt();
for (int c = shape; c >= 1; --c) {
for (int a = 1; a <= c; a++) {
System.out.print(" ");
}
for (int d = c; d <= shape; d++) {
System.out.print(d);
}
System.out.println(" ");
}
// result
// 5
// 45
// 345
// 2345
// 12345
关于java - 嵌套循环递减数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643577/