gpt4 book ai didi

C# 读取文本文件查找最小最大平均值

转载 作者:太空宇宙 更新时间:2023-11-03 21:12:41 27 4
gpt4 key购买 nike

我正在 TAFE 学习,但类(class)和我自己都没有从我的讲师那里得到任何帮助。

我需要从一个 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);

关于C# 读取文本文件查找最小最大平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36616958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com