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");
}
}
因此,我想做的是拥有一个与终端窗口的左边缘对齐的塔。但是由于某种原因,这会在“最后”行的开头(塔楼的底部)产生两个额外的空间。甚至更奇怪,当我用笔和纸坐下并手动运行该程序时,我也表明应该在第一行上包含等于“height” +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/