打开文件,读取数据和计算平均值的程序出错

时间:2016-04-16 16:01:26

标签: c#

我的编程课中给了一个直接读取的提示,

  

“在本练习中,一类学生的考试成绩存储在一个文件中。您要编写一个成功打开文件的程序,读入考试成绩,然后在控制台上输出。”

每次运行程序时都会出现错误,

  

“GradeCalculator.exe中发生了'System.IndexOutOfRangeException'类型的未处理异常”

错误出现在代码上,“gradesArray [i] = gradesArray [gradeData];                         我++;“

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace GradeCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\grades.txt";
            int gradeData;
            StreamReader grades = new StreamReader(folder);
            int[] gradesArray = new int[50];
            while (!grades.EndOfStream)
            {
                for (int i = 0; i != 50;)
                {
                    gradeData = int.Parse(grades.ReadLine());

                    gradesArray[i] = gradesArray[gradeData];
                    i++;
                }
            }

            Program calc = new Program();

            for (int i = 0; i < 50; i++) 
            {
                int average = calc.averageArr(gradesArray);
                Console.WriteLine("{0}", average);
                Console.ReadLine();
            }
        }

        public int averageArr(int[] arr)
        {
            int avg = 0;
            int numGrades = 0;
            int sum = 0;
            for (int i = 0; i < 50; i++)
            {
                do
                {
                    sum += arr[i];
                } while (arr[i] != 0);
            }
            avg = sum / numGrades;
            return avg;
        }
    }
}

有关如何解决此问题的任何建议吗?

来自档案的数据: 88 90 78 65 50 83 75 23 60 94

编辑新代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace GradeCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\grades.txt";
            int gradeData;
            StreamReader grades = new StreamReader(folder);
            int[] gradesArray = new int[50];
            do
            {
                gradeData = int.Parse(grades.ReadLine());
                for (int i = 0; i != gradesArray.Length;)
                {
                    gradesArray[i] = gradeData;
                    i++;
                }
            }
            while (!grades.EndOfStream);

            Program calc = new Program();

            int average = calc.averageArr(gradesArray);

             Console.WriteLine("{0}", average);
             Console.ReadLine();
        }

        public int averageArr(int[] arr)
        {
            int avg = 0;
            int sum = 0;

            for (int i = 0; i != arr.Length; )
            {
                sum += arr[i];
                i++;
            }

            avg = sum / arr.Length;
            return avg;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您有很多冗余代码,您只需使用Linq并获取int数组。

var intArray = File.ReadAllLines("filepath")
                   // Remove below line if you have only 1 number in each line, else specify delimiter and keep it.
                   .SelectMany(s=>s.Split(' '))                        
                   .Select(int.Parse);

获得数组后,Array具有返回Average

的函数
var average = intArray.Average();

<强>更新

由于你想编写计算平均值的函数,你可以这样做。请注意,我将一些变量类型修改为Decimal以避免int除法。

public decimal averageArr(int[] arr)
{
    decimal avg = 0;
    int numGrades = 0;
    decimal sum = 0;
    for (int i = 0; i < arr.Length; i++)
    {
        sum += arr[i];
    }

    avg = sum / arr.Length;
    return avg;
}