簡體   English   中英

CS50 馬里奧(更舒適)錯誤信息

[英]CS50 Mario (More Comfortable) Error Messages

我的代碼有什么問題? 我目前正在收到這些消息(悲傷的面孔是錯誤):

:) 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;
}

嘗試在用戶輸入高度后包含 if 語句。 這至少會讓你得到高度為 0 的正確結果。

if (height == 0)
{
    return 0;
}

這些 /messages 是修復代碼的關鍵。 你有這個。

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM