【发布时间】:2017-09-21 18:48:05
【问题描述】:
我目前正在编写一个简单的骰子游戏,我遇到了一个让我感到困惑的错误,这是我的代码。
foreach (var die in Dice.rolls)
{
Console.WriteLine(die.ToString());
}
if (player.score += score >= goal)
{
playing = false;
Console.WriteLine("{0} has won the game!", player.name);
Console.WriteLine("Please press any key to end the game");
Console.ReadKey();
}
else
{
player.score += score;
}
我遇到的问题是这条线:
if (player.score += score >= goal)
抛出一个错误,告诉我不能在 int 和 bool 上使用它,但 if 语句中的所有变量都是 int。此外,这里还有几行:
player.score += score;
没有给我任何错误。
【问题讨论】:
-
您不能在同一行中执行这两项操作。只需先添加分数,然后进行比较。您告诉编译器要做的是解决 score >= goal 并将其添加到 player.score,从而出现错误。
标签: c# runtime-error operators