[英]c# read text file find min max average
我正在TAFE學習,但是班級和我自己都沒有得到老師的幫助。
我需要讀取一個txt文件,並從中找到最小最大和平均值,然后將其打印到控制台。
先前的練習是從數組中獲取最小最大平均值,而我已經編寫了此代碼,並且效果很好。 我正在使用VS2012。
我已經編寫了讀取文本文件並將其打印到控制台的代碼-但我找不到最小最大值和平均值。 我得到“對象引用未設置為對象的實例”。 當我運行程序時。
請注意,我使用相同的代碼來查找數組中的最小最大平均值...我覺得這可能是個問題,但我無法解決!
這是我的數組代碼...
static void Main(string[] args)
{
int[] hoursArray = { 1, 24, 9, 7, 6, 12, 10, 11, 23, 8, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };
for (int i = 0; i < hoursArray.Length; i++)
{
Console.WriteLine(hoursArray[i].ToString());
}
{
{
int low = hoursArray[0];
for (int index = 1; index > hoursArray.Length; index++)
{
if (hoursArray[index] < low)
{
low = hoursArray[index];
}
}
Console.WriteLine("Lowest Hours Parked = " + low);
int high = hoursArray[0];
for (int index = 1; index < hoursArray.Length; index++)
{
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
Console.WriteLine("Highest Hours Parked = " + high);
int total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
}
average = (double)total / hoursArray.Length;
Console.WriteLine("Average Hours Parked =" + average.ToString("N"));
Console.ReadLine();
}
}
}
}
}
如前所述,這很好。 現在針對我的問題...我已經編寫了代碼,如下所示顯示文本文件中的數據並添加了注釋...
static void Main(string[] args)
{
StreamReader hours = new StreamReader("hours.txt");
string number = "";
while (number != null)
{
number = hours.ReadLine();
if (number != null)
Console.WriteLine(number);
}
//list of numbers above is all ok when running program
int total = 0;
double average = 0;
for (int index = 0; index < number.Length; index++)
{
total = total + number[index];
}
average = (double)total / number.Length;
Console.WriteLine("Average = " + average.ToString("N2"));
int high = number[0];
for (int index = 0; index < number.Length; index++)
{
if (number[index] > high)
{
high = number[index];
}
}
Console.WriteLine("Highest number = " + high);
int low = number[0];
for (int index = 0; index > number.Length; index++)
{
if (number[index] < low)
{
low = number[index];
}
}
Console.WriteLine("Lowest number = " + low);
hours.Close();
Console.ReadLine();
}
}
}
我建議使用Linq :
// First of all define the source - it can be an array, file - whatever:
// var source = hoursArray; // e.g. source for the array
var source = File
.ReadLines(@"C:\MyFile.txt") //TODO: put actual file here
.SelectMany(line => line.Split(',')) //TODO: put actual separator here
.Select(item => int.Parse(item));
// having got source (IEnumerable<int>) let's compute min, max, average
int max = 0;
int min = 0;
double sum = 0.0; // to prevent integer division: 7/2 = 3 when 7.0 / 2 = 3.5
int count = 0;
boolean firstItem = true;
foreach (item in source) {
sum += item;
count += 1;
if (firstItem) {
firstItem = false;
max = item;
min = item;
}
else if (item > max)
max = item;
else if (item < min)
min = item;
}
// Finally, formatted output
Console.Write("Min = {0}; Max = {1}; Average = {2}", min, max, sum / count);
使用File.ReadLines
讀取文件內容,然后使用簡單的Linq
語句將其轉換為int
數組。
int[] hoursArray = File
.ReadLines("filepath") // Read all lines,
.SelectMany(s => s.Split(",").Select(int.Parse)) // Split by ',' and convert them to int.
.ToArray();
完成此操作后,其余代碼應按原樣工作。
還有一個建議,在數組上有預定義的方法/函數來獲取Average
, Min
和Max
。
你可以做。
var avg = hoursArray.Average();
var min = hoursArray.Min();
var max = hoursArray.Max();
string text = System.IO.File.ReadAllText("filePath");
int[] hoursArray = (text.Split(' ').ToArray()).Select(x => Convert.ToInt32(x)).ToArray();
int max = hoursArray.Max();
int min = hoursArray.Min();
double avg = hoursArray.Average();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.