gpt4 book ai didi

CS50 Mario Pyramid - 代码有效,但运行 check50 时仍然出现错误

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

当我运行此代码时,我确实得到了所需的右对齐金字塔结果,该结果随用户输入的高度而变化。但是,当我运行 CS50 检查功能时,它告诉我以下内容:

:( 正确处理高度 1 \预期输出,但不是“##\n”

:( 正确处理高度 2 \预期输出,但不是“##\n ###\n”

:( 正确处理高度 23 \预期输出,但不是“##\n ...”

:( 拒绝高度 24,然后接受高度 2 \预期输出,但不是“##\n ###\n”

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

int main(void)
{
int height;
int row;
int space;
int hash;
// declare variables
do
{
printf("Height:\n");
height = GetInt();
}
while (height < 0 || height > 23);

// build pyaramid if acceptable height entered

for (row = 0; row < height; row++)
{

for (space = 0; space < (height - row); space++)
{
printf(" ");
}
for (hash = 0; hash < 2 + row; hash++)
{
printf("#");
}
printf("\n");
}

}

最佳答案

你确定你得到了正确的空格输出吗?我记得 Mario.c 应该输出一个右对齐的金字塔,但左下角也与终端侧对齐。可能很难说,因为它是空的。我建议通过在其位置打印一个字符来测试它

例如

for (space = 0; space < (height - row); space++)
{
printf("b");
}

这应该会让您了解需要更改哪些内容。

关于CS50 Mario Pyramid - 代码有效,但运行 check50 时仍然出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39863776/

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