【发布时间】:2018-06-21 06:03:24
【问题描述】:
//Aim of this program is to print a hash pyramid
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int height, spaces, hash;
do
{
printf("Enter the height of the wall:\n"); // Prompt the user for the height
height = get_int();
}
while(height<0 || height>23);
for (int i=0; i<height; i++)
{
for (spaces= height-i; spaces>1; spaces--)
{
printf(" "); //print spaces
for (hash= 0; hash<=height+1; hash++)
{
printf("#"); //print hashes
}
printf("\n"); //move to the next line
}
}
}
这是一个打印散列金字塔的程序。 我这样做是我在 edx 上的 CS50x 课程的一部分,所以我包含了 CS50 库。现在,关于程序,我知道我在第三个“for 循环”中搞砸了一些东西,但我没有知道它是什么。
有人可以帮我解决这个问题吗?
【问题讨论】:
-
while(h<0 || h>23);中的h是什么? -
如果用户输入例如数字 3,预期的输出是什么?
-
你的第三个 for 循环应该在第二个 for 循环之后,而不是在里面。
-
@Adit 总是发布真实代码
-
这很好奇;你已经接受了我说你应该做的答案,所以除非你的循环限制是关闭的,否则我建议的是你认为必要和可接受的。