我目前正在编写一个简单的骰子游戏,并遇到一个让我困惑的错误,这是我的码。CS0019 t运算符'+ ='不能应用于'int'和'bool'类型的操作数
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)
抛出了一个错误,告诉我,我不能用它在廉政局和布尔的,但所有的变量if语句是INT的。此外在这里几行:
player.score += score;
不给我任何错误。
更多信息,您不能在同一行做这两种操作。只需先添加分数,然后再进行比较。你告诉编译器要做的是解决score> = goal并将其添加到player.score中,从而产生错误。 –