2016-03-23 15 views
-1

以下私は私のファイル(hours.txt)から読み込む小さいC#プログラムを持っていますが、計算エリアを修正する問題があります。私は結果が出てこないようにいくつか修正しようとしました。だから誰もこれを修正する方法を知っていますか?C#コードエラー(計算)

あなたのコードをオフに基づいて、それはあなたのhoursArrayためだように見える、高、低、および平均値は、すべての文字列である推測
 //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

これはOKです。 'string average = hoursArray [0];'? 'string average = hoursArray [2];' –

+0

でなければ、 'while'ループを' do..while'ループに変更することでコードを改善することができます。 – Shanid

答えて

0

- これらは数値でなければなりません。

にあなたの変数宣言を変更:ファイルから読んでいるときに、

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

注:私はこのコードをテストしていません。