我需要能够将文本文件读入数组,而不是自己输入所有的值。 文本文件读作:将.txt文件读入数组
8.7
9.3
7.9
6.4
9.6
8.0
8.8
9.1
7.7
9.9
5.8
6.9
该程序的主要目的是从数据文件中读取分数,将它们存储在一个数组中,并计算12个分数的最高,最低,总和平均值。
该文本文件存储在项目的Debug文件夹中。
这是我迄今所做的:
Console.WriteLine("Numbers in the list: " + scores.Length);
//highest number
double high = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] > high)
{
high = scores[index];
}
}
Console.WriteLine("Highest number = " + high);
//lowest number
double low = scores[0];
for (int index = 1; index < scores.Length; index++)
{
if (scores[index] < low)
{
low = scores[index];
}
}
Console.WriteLine("lowest number = " + low);
//average of the scores
double total = 0;
double average = 0;
for (int index = 0; index < scores.Length; index++)
{
total = total + scores[index];
}
average = (double)total/scores.Length;
Console.WriteLine("Total = " + total);
Console.WriteLine("Average = " + average.ToString("N2"));
Console.ReadKey();
}
}
}
'var lines = File.ReadAllLines(@“C:\ scores。';' – sed 2014-09-29 13:24:50
'string [] lines = File.ReadAllLines(path); double [] values = Array.ConvertAll(lines,double.Parse);' – 2014-09-29 13:25:01
numbers.Min,numbers.Max,numbers.Average。http ://msdn.microsoft.com/en-us/library/bb386914(v = vs.110).aspx – Neolisk 2014-09-29 13:25:22