gpt4 book ai didi

c - 为什么我的 for 循环打印不像我期望的伪代码那样?

转载 作者:太空狗 更新时间:2023-10-29 17:12:27 28 4
gpt4 key购买 nike

int main(void)
{
int height = 24;

while (height > 23 || height <=0)
{
printf("How tall do you want the tower to be?\n");
height = GetInt();
}

for(int a = 0; a < height; a++)
{
for(int c = a; c<=height; c++)
{
printf(" ");
}
for(int b = height - a; b<=height; b++)
{
printf("#");
}
printf("\n");
}
}

所以,我要做的是让塔与终端窗口的左边缘对齐。但是出于某种原因,这会在“最后”行(塔的底部)的开头生成两个额外的空格。更奇怪的是,当我坐下来用笔和纸手动运行程序时,我发现第一行应该有一些等于“高度”+ 1 的空格,后跟一个“#”然后是一个新行,然后是一些等于“height”的空格,后跟两个“#”,依此类推。为什么我的代码不是这样计算的,我的两个额外空格是怎么回事?抱歉解释不当。

最佳答案

这是因为您在每行的开头打印 height + 1 而您想打印 height-1 空格。

改变你的条件:

for(int c = a; c<=height; c++)

for(int c = a; c<height-1; c++)

关于c - 为什么我的 for 循环打印不像我期望的伪代码那样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799313/

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