方法重载方法的错误消息

本文关键字:方法 消息 错误 重载 | 更新日期: 2023-09-27 18:31:32

如您所知,我是 C# 的新手,我关注了一个 youtube 视频,我似乎不明白为什么我的方法会收到此错误消息。我知道任何比我更了解的人都会或应该能够立即确定错误,所以我在这里发布了我正在使用的代码。

任何建议,教程或任务将不胜感激,并欢迎建设性的批评。

namespace AverageScore
{
 class Program
 {
    static void Main(string[] args)
    {
        int Score;
        List<int> scores = new List<int>();
        Console.WriteLine("Please Enter Your Scores");

        string input = "";
        while (input != "stop")
        {
            input = Console.ReadLine();
            int result = 0;
            if (int.TryParse(input, out result))
            {
                scores.Add(result);
            }
            else
            {
                Console.WriteLine(input + " Is Not A Valid Integer");
            }
        }
        Console.WriteLine("Your Score Is: " + CalculateAverage(Score));
        Console.Read();
    }
    static int CalculateAverage(List<int> Score)
    {
        int result = 0;
        foreach (int i in Score)
        {
            result += i;
        }
        return result / Score.Count;
    }
}

}

方法重载方法的错误消息

按如下方式更正此行:-

Console.WriteLine("Your Score Is: " + CalculateAverage(scores));
Console.Read();

您的方法CalculateAverage期望List<int>但您传递的int值"分数"。

编辑:
除了这个例外,我注意到你没有在你的 else 代码块中处理"停止",所以当用户说"停止"时,你的程序会说 - stop 不是一个有效的整数,可能你不希望这样,因此在你的 else 部分添加以下代码块:-

else
{
    if (input == "stop")
        break;
    Console.WriteLine(input + " Is Not A Valid Integer");
}

此外,如果要计算平均值,则计算平均值方法的返回类型应decimal,而不是int