Why isn't my for loop printing like I would expect from my pseudocode?
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");
}
}
So, what I'm trying to do is have a tower that aligns with the left edge of the terminal window. For some reason though, this generates two extra spaces at the beginning of the "last" line (bottom of the tower). Even weirder, when I sit down with a pen and paper and manually run through the program, I'm showing that there should be on the first line, a number of spaces equal to "height" + 1, followed by one "#" then a new line, then a number of spaces equal to "height" followed by two "#", and so on. Why isn't my code evaluating like that, and what's with my two extra spaces? Sorry for the poor explanation.
It's because you print height + 1
at the beginning of each line whereas you want to print height-1
spaces.
Change your condition from:
for(int c = a; c<=height; c++)
to
for(int c = a; c<height-1; c++)
链接地址: http://www.djcxy.com/p/77488.html
上一篇: 在当前的Twig模板中使用自定义分隔符