我需要能夠將文本文件讀入數組,而不是自己輸入所有的值。 文本文件讀作:將.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