0

誰かが助けることができますか。これらの 2 行のコードでエラーが発生します。 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

If that's a C-like language, you need to use == for equality checks, not =. The single = is for assignment so that:

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

is okay, but:

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

will, even if it compiles, not do what you expect.

You have a classic example in your code. The segment:

if (count_black = 0) blah;

will not execute blah when count_black is zero. It will set count_black to zero and steadfastly refuse to ever execute blah, since the result of count_blah = 0 is 0 (false).


If you want the equality:

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 に答える