-1
下面我有一个小的C#程序,它从我的文件(hours.txt)中读取,但我遇到了修复//计算区域的问题。我已经尝试了几件事情来解决它,没有任何结果,所以这里的任何人都不知道如何解决这个问题?C#代码错误(计算)
//Declare ints
string [] hoursArray = new string[30];
string high = hoursArray[0];
string low = hoursArray[1];
string average = hoursArray[0];
int total = 0;
double avarage = 0;
//Input
StreamReader fileSR = new StreamReader("hours.txt");
int coutner = 0;
string line;
line = fileSR.ReadLine();
while (line != null) //check while not EOF
{
hoursArray[total] = line;
total++;
line = fileSR.ReadLine();
}
fileSR.Close();
for (int index = 0; index < hoursArray.Length; index++)
{
Console.WriteLine(hoursArray[index]);
}
//Calculate
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
if (hoursArray[index] < low)
{
low = hoursArray[index];
}
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
avarage = (double)total/hoursArray.Length;
//Output
Console.WriteLine("Total hours parked: " + total);
Console.WriteLine("Avarage hours parked: " + avarage.ToString("N2"));
Console.WriteLine("Lowest number = " + low);
Console.WriteLine("Lowest number = " + high);
Console.ReadKey();
是这个行'string average = hoursArray [0];'?不应该是'string average = hoursArray [2];' –
您可以通过将'while'循环更改为'do..while'循环来改进代码。 – Shanid