0

有人可以帮忙吗?这两行代码出错。 num_red - count_red = red_pot;// all defined as 0

while (count_red = 0 && count_yellow = 0 && count_green = 0 && count_brown = 0 && count_blue = 0 && count_pink = 0)
        {
            if (count_black = 0)
            {
                score = score + 7;
                printf("Score: %d\n", score);
                num_balls = num_balls - 1;
            }

        }
4

1 回答 1

5

如果那是一种类似 C 的语言,则需要==用于相等检查,而不是=. 单曲=用于分配,以便:

int seven = 7;
int five = 5;
if (seven - five == 2) ...

没关系,但是:

int seven = 7;
int five = 5;
if (seven - five = 2) ...

即使它编译,也不会做你所期望的。

您的代码中有一个经典示例。细分:

if (count_black = 0) blah;

为零时不会执行blahcount_black它将设置 count_black为零并坚决拒绝执行blah,因为结果count_blah = 0为 0(假)。


如果你想要平等:

num_red - count_red == red_pot

to be true, you need to assign one of those variables (the "unknown" one) based on the other two "known" ones. For example, if num_red and count_red are known, set red_pot with:

red_pot = num_red - count_red;

Alternatively, if red_pot and count_red are known, set num_red with:

num_red = count_red + red_pot;
于 2011-11-02T03:08:36.670 回答