[英]CS50 Mario (More Comfortable) Error Messages
What is wrong with my code?我的代码有什么问题? I'm currently getting these messages (sad faces are the errors):
我目前正在收到这些消息(悲伤的面孔是错误):
:) 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 ""
The program seems to work fine other than being exactly the way CS50 is looking for.该程序似乎运行良好,而不是完全符合 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;
}
Try including an if statement after the user inputs height.尝试在用户输入高度后包含 if 语句。 That will at least get you the proper result for a height of 0.
这至少会让你得到高度为 0 的正确结果。
if (height == 0)
{
return 0;
}
Those /messages are key to fixing your code.这些 /messages 是修复代码的关键。 You've got this.
你有这个。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int number;
do
{
number = get_int("Height:");
}
while (number < 1 || number > 8);
for (int i = 0; i < number; i++)
{
for (int j = number - i - 2; j >= 0; j--)
{
printf(" ");
}
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf(" ");
for (int k = 0; k <= i; k++)
{
printf("#");
}
printf("\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.