有没有人能帮我写一段代码来打印下面的模式。
987654321
98765432
9876543
987654
98765
9876
987
98
9 这是我的代码示例,但我得到的结果与上面的模式正好相反。
height = getInPut("Enter the height of the triangle");
int h = Integer.parseInt(height);
int start = h, num=1,max=h;
for (int r= 1; r <=h; r++)
{
System.out.println();
for (int j = 1; j<= max; j++)
{
if(h>=6 && h<=10)
{
System.out.print(num);
}else{
System.out.println("height should be between 6-10");
System.exit(0);
}
num++;
}
num= r+1;
max--;
}发布于 2018-12-03 02:33:38
我将从StringBuilder中987654321的初始值开始,然后在包含字符的情况下循环;在每次循环中,我们希望打印StringBuilder的初始长度(9)和当前长度之间的差值(以空格表示),然后在删除最后一个字符之前打印StringBuilder的内容。喜欢,
StringBuilder sb = new StringBuilder("987654321");
while (sb.length() > 0) {
for (int i = 0; i < 9 - sb.length(); i++) {
System.out.print(' ');
}
System.out.println(sb);
sb.setLength(sb.length() - 1);
}哪些输出(根据请求)
987654321
98765432
9876543
987654
98765
9876
987
98
9我将把填充初始StringBuilder和将9调整为一个变量作为练习留给读者。
https://stackoverflow.com/questions/53583284
复制相似问题