【问题标题】:left operand must be l-value左操作数必须是左值
【发布时间】:2011-11-02 03:01:57
【问题描述】:

有人可以帮忙吗?这两行代码出错。 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;
            }

        }

【问题讨论】:

  • 几乎所有你有=,你想要==
  • 您不能为表达式num_red - count_red 赋值。表达式的值由算术规则决定,而不是您的代码。
  • Anthony- 这是我的一个愚蠢的错误。谢谢,
  • Matt-关于如何解决 num_red - count_red = red_pot 的任何想法
  • @user,用简单的语言写下你想要的表达方式怎么样。然后我们也许可以告诉你怎么写。

标签: c operand


【解决方案1】:

如果那是类 C 语言,则需要使用 == 进行相等检查,而不是 =。单个= 用于assignment,因此:

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;

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


如果你想要平等:

num_red - count_red == red_pot

确实,您需要根据其他两个“已知”变量分配其中一个变量(“未知”变量)。例如,如果 num_redcount_red 已知,则将 red_pot 设置为:

red_pot = num_red - count_red;

或者,如果red_potcount_red 已知,则将num_red 设置为:

num_red = count_red + red_pot;

【讨论】:

  • 非常感谢大家。错误现在消失了。非常感激。欢呼伙计们
猜你喜欢
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多