2016-03-23 143 views
-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(); 
+0

是这个行'string average = hoursArray [0];'?不应该是'string average = hoursArray [2];' –

+0

您可以通过将'while'循环更改为'do..while'循环来改进代码。 – Shanid

回答

0

基于猜测了你的代码,但它看起来像那是因为你的hoursArray,高,低和平均值都是字符串 - 这些都应该是数字。

修改您的变量声明:

List<double> hoursArray = new List<double>; // changing this to a list 
    double high = double.MinValue; // initialize at the min for initial comparison 
    double low = double.MaxValue; // initialize at the max 
    double total = 0; 
    double avarage = 0; 

当你从文件中读取,转换成字符串值,你读双打:

//Input 
    StreamReader fileSR = new StreamReader("hours.txt"); 
    string line = fileSR.ReadLine(); 
    while (line != null) //check while not EOF 
    { 
     double hours = 0; 
     if (double.TryParse(line, out hours)) // check if line is a valid double 
     { 
      hoursArray.Add(hours); 
     } 
     line = fileSR.ReadLine(); 
    } 
    fileSR.Close(); 

现在,你可以使用号码您的计算部分而不是字符串:

for (int index = 0; index < hoursArray.Count; index++) 
    { 
     Console.WriteLine(hoursArray[index]); 
    } 

    //Calculate 
    for (int index = 0; index < hoursArray.Count; index++) 
    { 
     total = total + hoursArray[index]; 
     if (hoursArray[index] < low) 
     { 
      low = hoursArray[index]; 
     } 
     if (hoursArray[index] > high) 
     { 
      high = hoursArray[index]; 
     } 
    } 
    avarage = (double)total/hoursArray.Count; 

    //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(); 

注:我没有测试过这个代码。