在C中写一个for循环

发布于 2025-01-17 18:20:38 字数 658 浏览 4 评论 0原文

每个人!我正在尝试解决 cs50 中的马里奥问题,当我插入这样的 for 循环时:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int height;
    int i = 0;
    int j = 9;

    do
    {
        height = get_int("establish the height of the pyramide: ");
    }
    while (height < 1 || height > 8);
    printf("you select the height of: %i\n", height);

    for (i < height; i++)
    {
        for (j > height; j++)
        {
            printf("#");
        }
    }
}

我收到这样的错误:

mario.c:17:12: 错误:关系比较结果未使用 [-Werror,-Wunused-比较] 对于(i <= 高度;i++):

我已经尝试在循环内声明 i,但产生的错误是相同的。我在这里做错了什么?

everyone! i am trying to resolve a mario problem in cs50, and when i insert a for loop like that:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int height;
    int i = 0;
    int j = 9;

    do
    {
        height = get_int("establish the height of the pyramide: ");
    }
    while (height < 1 || height > 8);
    printf("you select the height of: %i\n", height);

    for (i < height; i++)
    {
        for (j > height; j++)
        {
            printf("#");
        }
    }
}

i get an error like that:

mario.c:17:12: error: relational comparison result unused
[-Werror,-Wunused-comparison]
for (i <= height; i++):

i've already tried declaring i inside the loop, but the resulting error is the same. what am i doing wrong here?

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

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

发布评论

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

评论(1

翻了热茶 2025-01-24 18:20:38

在C中,定义了以下方式

for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

,即它具有三个部分。第一部分是表达式(可能省略)或声明。

因此,这些循环

for (i < height; i++)
for (j > height; j++)

不满足语法。而且,第二个循环中的状况

j > height

没有意义。

还要尝试在使用它们的最小范围中定义变量。

看来您需要的是以下内容

for ( int i = 0 ; i < height; i++)
{
    for ( int j = 0; j < i + 1; j++)
    {
        putchar( '#' );;
    }
    putchar( '\n' );
}

,或者可能是以下内容

for (int i = 0; i < height; i++)
{
    printf( "%*c", height - i, '#' );
    for (int j = 1; j < i + 1; j++)
    {
        putchar( '#' );;
    }
    putchar( '\n' );
}

In C the for loop is defined the following way

for ( expressionopt ; expressionopt ; expressionopt ) statement
for ( declaration expressionopt ; expressionopt ) statement

that is it has three parts. The first part either is an expression (possibly omitted) or a declaration.

So these for loops

for (i < height; i++)
for (j > height; j++)

do not satisfy the grammar. Moreover the condition in the second loop

j > height

does not make a sense.

Also try to define variables in the minimal scope where they are used.

It seems what you need is something like the following

for ( int i = 0 ; i < height; i++)
{
    for ( int j = 0; j < i + 1; j++)
    {
        putchar( '#' );;
    }
    putchar( '\n' );
}

or maybe like the following

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