gpt4 book ai didi

CS 50- Pset 1 马里奥程序

转载 作者:行者123 更新时间:2023-11-30 15:32:52 30 4
gpt4 key购买 nike

问题集要求我们使用哈希创建一个半金字塔。这是它的外观图像的链接 -

enter image description here

我明白了这个想法并编写了程序,直到打印空格(我已将其替换为“_”,以便我可以测试它的前半部分。

但是,当我尝试运行我的程序时,它不会超出 do-while 循环。换句话说,它一直询问我金字塔的高度,并且似乎根本不运行 for 循环。我尝试了多种方法,但这个问题似乎仍然存在。

如有任何帮助,我们将不胜感激!

下面是我的代码 -

# include <cs50.h> 
# include <stdio.h>

int main(void)

{
int height;

do
{
printf("Enter the height of the pyramid: ");
height = GetInt();
}
while (height > 0 || height < 24);

for (int rows = 1; rows <= height, rows++)
{
for (int spaces = height - rows; spaces > 0; spaces--)
{
printf("_");
}
}
return 0;
}

运行该程序会产生以下输出 -

Enter the height of the pyramid: 11
Enter the height of the pyramid: 1231
Enter the height of the pyramid: aawfaf
Retry: 12
Enter the height of the pyramid:

最佳答案

您的 do/while 循环条件不正确 - 更改:

do {
...
} while (height > 0 || height < 24);

到:

do {
...
} while (height <= 0 || height >= 24);

或者:

do {
...
} while (!(height > 0 && height < 24));

(无论您认为哪个更具可读性/直观)。

关于CS 50- Pset 1 马里奥程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23951034/

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