方法重载方法的错误消息

时间:2014-11-22 06:47:19

标签: c# .net methods static

我是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;
    }
}

}

1 个答案:

答案 0 :(得分:2)

按如下方式更正此行: -

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

您的方法CalculateAverage期待List<int>,但您传递int值“得分”。

修改
除了这个例外,我注意到你没有在你的else代码块中处理“停止”,所以当用户说“停止”时,你的程序会说 - 停止不是一个有效的整数,可能你不想要这样,在你的其他部分添加以下代码块: -

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

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