【问题标题】:cs50 mario less problem - my code is printing in a single line. I cant find whats wrong with itcs50 mario less 问题 - 我的代码在一行中打印。我找不到它有什么问题
【发布时间】:2023-01-09 15:55:21
【问题描述】:
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n;
    do
    {
        n = get_int("Height: ");
    }
    while ((n < 1) || (n > 8));
for
    (int i = 0; i < n; i++)

    {for (int j = 0; j < n; j++)
       if(i+j < n-1)
        printf(" ");
        else
        printf("#");
        }
        printf("\n");
}

【问题讨论】:

  • Prateek,这是作业/作业问题吗?您是否联系过相应的讨论论坛?如果这不是一个选项,并且如果可以在这里询问 - 您能否在粘贴到此处之前正确格式化您的代码,显示当前输出、预期输出以及您对哪一部分有疑问?

标签: cs50


【解决方案1】:

确保使用 {} 来包含代码块每一个for循环和if条件。这个printf(" ");外部for 都块,因此只打印一次。 (最后一个printf 之前的右大括号} 关闭i 循环的代码块)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多