CS50马里奥的问题少 - 我的代码是在一行中打印的。我找不到它怎么了

发布于 2025-02-06 12:06:09 字数 417 浏览 1 评论 0原文

#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");
}
#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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-02-13 12:06:11

确保使用{}将每个 循环和 if condice> condice> condiflational 包围。此printf(“ \ n”); 两个的 blocks ,因此准确打印一次。 (闭合支架}在最终printf关闭i循环的代码块)。

Make sure you use {} to enclose codes blocks for every for loop and if conditional. This printf("\n"); is outside both for blocks and thus prints exactly once. (The closing brace } before the final printf closes the code block of the i loop).

临走之时 2025-02-13 12:06:11

您的代码格式不佳,这使您自己很难:

#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");
}

因此,您运行循环,然后打印一个新的新线。目前尚不清楚您是否需要:

  1. 每个“”和“#”之后的newline:
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            printf("%c\n", i + j < n - 1 ? ' ' : '#');
  1. 每个'i'的newline:
   for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            printf("%c", i + j < n - 1 ? ' ' : '#');
        printf("\n");
   }

Your code is formatted poorly which makes it hard for yourself:

#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");
}

So you run you loops then print a single newline. It's not clear if you want:

  1. newline after each " " and "#":
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            printf("%c\n", i + j < n - 1 ? ' ' : '#');
  1. newline for each 'i':
   for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            printf("%c", i + j < n - 1 ? ' ' : '#');
        printf("\n");
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文