gpt4 book ai didi

java - 反转 while 循环形状?

转载 作者:行者123 更新时间:2023-11-30 03:10:53 28 4
gpt4 key购买 nike

所以我试图反转我当前制作的形状的输出。我想知道我应该扭转这种情况吗?我尝试更改变量“a”和“c”的值,最终陷入无限循环。

class IRT {

public static void main(String[] args) {

int a = 2;
int b;
int c = 6;

do {
b = a - 1;
if (b != 0) {
do {
System.out.print("*");
b--;

} while (b >= 1);
}
b = 1;
do {
System.out.print(" ");
b++;

} while (b <= (c - a + 1));

System.out.println();
a++;
} while (a <= c);

}

}

XML 代码

   My output                    What i'm trying to get

* *
** **
*** ***
**** ****
***** *****

最佳答案

如果您只想反转每行的顺序(即首先打印空格,然后打印 *),则只需将顺序更改为两个内部 while 循环.

  int a=2;
int b;
int c=6;

do{
b=1;
do{
System.out.print(" ");
b++;

}while(b <= (c - a + 1));

b = a - 1;
if (b!=0){
do{
System.out.print("*");
b--;

}while(b >= 1);
}

System.out.println();
a++;

} while(a <= c);

附注缩进代码使其更容易理解。

输出:

     *
**
***
****
*****

关于java - 反转 while 循环形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com