运算符<不能应用于“方法组”类型的操作数

时间:2019-01-17 10:39:39

标签: c#

我试图将整个游戏循环变成一种方法。

我有两种计数方法。

 public void CountScoreForPlayerOne()
        {
            Score++;
        }
        public void CountScoreForPlayerTwo()
        {
            Score++;
        }

当我尝试比较分数时。

if (CountScoreForPlayerOne > CountScoreForPlayerTwo)
            {
                Console.WriteLine("There are no more cards the game is over!");
                Console.WriteLine($"{FirstPlayer} won the game");
            }
            else if (CountScoreForPlayerOne < CountScoreForPlayerTwo)
            {
                Console.WriteLine("There are no more cards the game is over!");
                Console.WriteLine($"{SecondPlayer} won the game");
            }
            else
            {
                Console.WriteLine("There are no more cards the game is over!");
                Console.WriteLine("None won the game");
            }

            Console.WriteLine($"The final score is {FirstPlayer} {Score} - {SecondPlayer} {Score}");

它表示运算符'>'不能应用于'method group'和'method group'类型的操作数。我不知道如何解决这个问题。

谢谢

2 个答案:

答案 0 :(得分:1)

public int CountScoreForPlayerOne()
{
    return PlayerOneScore++;
}

public int CountScoreForPlayerTwo()
{
    return PlayerTwoScore++;
}


if (CountScoreForPlayerOne() > CountScoreForPlayerTwo())

if (PlayerOneScore > PlayerTwoScore)

答案 1 :(得分:0)

我认为您的方法应将返回类型设置为int,例如:

 public int CountScoreForPlayerOne()
    {
        return Score++;
    }
    public int CountScoreForPlayerTwo()
    {
        return Score++;
    }

然后您可以像这样比较它们:

if (CountScoreForPlayerOne() > CountScoreForPlayerTwo())
        {
            Console.WriteLine("There are no more cards the game is over!");
            Console.WriteLine($"{FirstPlayer} won the game");
        }
        else if (CountScoreForPlayerOne() < CountScoreForPlayerTwo())
        {
            Console.WriteLine("There are no more cards the game is over!");
            Console.WriteLine($"{SecondPlayer} won the game");
        }
        else
        {
            Console.WriteLine("There are no more cards the game is over!");
            Console.WriteLine("None won the game");
        }

        Console.WriteLine($"The final score is {FirstPlayer} {Score} - {SecondPlayer} {Score}");