我的代码出了什么问题?我目前收到这些消息(悲伤的面孔是错误):
:) mario.c exists
:) mario.c compiles
:) rejects a height of -1
:( handles a height of 0 correctly
\ expected an exit code of 0, not output of " \n"
:( handles a height of 1 correctly
\ expected output, but not " \n# #\n"
:( handles a height of 2 correctly
\ expected output, but not " \n # #\n## ##\n"
:( handles a height of 23 correctly
\ expected output, but not " \n ..."
:) rejects a height of 24
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
该程序似乎工作正常,而不仅仅是CS50正在寻找的方式。
#include <stdio.h>
#include <cs50.h>
int main(void) {
int height;
int i;
do {
printf("Height:");
height = get_int();
}while(height < 0 || height > 23);
int x = height;
int y = 0;
for (i = 0; i < height + 1; i++)
{
for (i = 0; i < x; i++)
{
printf("%s", " ");
}
for (i = 0; i < y; i++)
{
printf("#");
}
printf("%s", " ");
for (i = 0; i < y; i++)
{
printf("#");
}
printf("\n");
x = x - 1;
y = y + 1;
}
return 0;
}
答案 0 :(得分:0)
在用户输入高度后尝试包含if语句。这至少可以让你获得高度为0的正确结果。
if (height == 0)
{
return 0;
}
这些/消息是修复代码的关键。你有这个。