我有一个输入文件,其中我有主要分数和次要分数。我需要找到主要分数的平均值。
我能够阅读主要和次要分数并将它们添加到列表中。但在计算平均值时,我在 s1.Average(PrimaryScore);
一行上收到错误我附加了program.cs和回归.cs类。谢谢你的帮助。
class Program
{
static Regression s1;
static void Main(string[] args)
{
int numRecords = 0;
List<double> PrimaryScore = new List<double>();
List<double> SecondaryScore = new List<double>();
FileStream fs = null;
Console.Write("Enter the file path: ");
while (fs == null)
{
string fileName = Console.ReadLine();
fs = OpenFileForRead(fileName);
}
StreamReader sr = new StreamReader(fs);
ReadFirstValueAsInt(sr, ref numRecords);
sr.ReadLine();
for (int i = 1; i <= numRecords; i++)
{
ReadRecordAsDoubleList(sr, PrimaryScore);
ReadRecordAsDoubleList(sr, SecondaryScore);
Console.WriteLine("Primary Data set {0}: ", i);
foreach (double d in PrimaryScore)
Console.Write(d + " ");
Console.WriteLine();
Console.ReadLine();
PrimaryScore.Clear();
sr.ReadLine();
Console.WriteLine("Secondary Data set {0}: ", i);
foreach (double d in SecondaryScore)
Console.Write(d + " ");
Console.WriteLine();
Console.ReadLine();
SecondaryScore.Clear();
}
s1 = new Regression();
s1.Average(PrimaryScore); //getting an error here
}
public static FileStream OpenFileForRead(string fileName)
{
try
{
return new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
catch (Exception e)
{
Console.Write("Problem opening file {0}, please enter a valid path: ", fileName);
}
return null;
}
public static void ReadFirstValueAsInt(StreamReader sr, ref int numRecords)
{
try
{
numRecords = Int32.Parse(sr.ReadLine());
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
public static void ReadRecordAsDoubleList(StreamReader sr, List<double> cr)
{
try
{
string[] doubles = sr.ReadLine().Split(new char[] { ' ' });
foreach (string s in doubles)
cr.Add(Double.Parse(s));
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
}
从程序类
调用的回归类class Regression
{
public static double Average(List<double> PrimaryScore)
{
int sum = 0;
foreach (int element in PrimaryScore)
sum += element;
return (double)sum / PrimaryScore.Count;
}
}
答案 0 :(得分:0)
Average
被声明为静态方法,因此您使用Regression.Average(PrimaryScore)
进行调用,或者从方法定义中删除static
修饰符,以便您可以使用{{s1.Average(PrimaryScore)
调用它。 1}}。
比如你的方法定义就像这样:
public static double Average(List PrimaryScore)
{
.
.
.