2014-11-22 68 views
0

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

}

回答

2

更正此行,如下所示: -

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

你的方法CalculateAverage期待List<int>但你传递一个int值 “分数”。

编辑:
除了这个之外,我已经注意到了,你是不是在代码的其他块处理“停止”,因此当用户说“停”,你的程序会说 - 停止是不是一个有效的整数,也许你不;吨希望如此,因此添加的代码以下块在你的其他部分: -

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

另外,如果你正在计算平均您CalculateAverage方法的返回类型应该是decimal而不是int

+0

谢谢,我对于是否把属于CalculateAverage的'Score'并没有真正记得分数感到困惑。谢谢拉胡尔。 – connormcwood 2014-11-22 06:53:32

+0

@connormcwood - 希望有所帮助!如果是这样,请将它标记为答案:) – 2014-11-22 20:00:33