PopulateStudents方法的问题

时间:2019-05-17 16:13:34

标签: c# database

我必须编写“ populatestudents”方法的内容,以允许程序打印来自txt文件“数据库”的信息。我继续越界错误或没有对象错误。我不确定在这方面哪里出了问题。除了那种方法外,其他所有方法都是由老师写的,所以我不确定在这里我缺少什么。

我编写了如下代码,但是继续得到错误。

class StudentS
    {
        Student s = new Student();
        const int NO_OF_STUDENTS = 5;
        List<Student> theStudentList;
        string fileName = "grades.txt";
        string[] temp = new string[23];
        //Student[] student = new Student[NO_OF_STUDENTS];

        public string PopulateStudents(string path)
        {
            int index = 0;

            theStudentList = new List<Student>();
            StreamReader filStream = new StreamReader(fileName);
            fileName = filStream.ReadLine();

            while (fileName != null)
            {
                temp = fileName.Split(',');
                theStudentList[index] = new Student();

                for (int x = 0; x < ListLength; x++)
                {

                    theStudentList.ElementAt(index).ID = temp[0];
                    theStudentList.ElementAt(index).NameLast = temp[1];
                    theStudentList.ElementAt(index).NameFirst = temp[2];

                }

                index++;
                fileName = filStream.ReadLine();

            }

            s.CalGrade();

            return path;
        } 

        public int ListLength
        {
            get { return theStudentList.Count; }
        }

        public string StudentID(int index)
        {
            return theStudentList.ElementAt(index).ID;
        }

        public string StudentLastName(int index)
        {
            return theStudentList.ElementAt(index).NameLast;
        }

        public string StudentGrade(int index)
        {

            return theStudentList.ElementAt(index).LetterGrade;
        }

        public float StudentAverage(int index)
        {

            return theStudentList.ElementAt(index).Average;
        }

    }

这是GradesUI;

  class GradesUI
    {
        StudentS myStudentS;

        public void MainMethod()
        {
            StudentS myStudentS = new StudentS();
            myStudentS.PopulateStudents("path");

            try
            {
                DisplayInfo();
            }

            catch (System.IO.IOException exc)
            {
                Console.WriteLine("\n\n");
                Console.WriteLine(exc.Message);
            }
        }

        void DisplayInfo()
        {
            Console.WriteLine("Student id\tLast Name\tAverage  \tGrade");

            for (int index = 0; index < myStudentS.ListLength; index++)
            {

                Console.WriteLine(" {0} \t {1}    \t {2}    \t {3}", myStudentS.StudentID(index), myStudentS.StudentLastName(index), myStudentS.StudentAverage(index), myStudentS.StudentGrade(index));
            }
        }
    }

这是学生班:

class Student
    {
        //data member
        string nameFirst;
        string nameLast;
        string studentID;
        List<int> earned;
        List<int> possible;
        float average;
        string letterGrade;

        public Student() //default constructor
        {
            studentID = null;
            earned = new List<int>();
            possible = new List<int>();
        }
        public Student(string id)  //overloaded constructor
        {
            studentID = id;
            nameFirst = null;
            nameLast = null;
            earned = new List<int>();
            possible = new List<int>();
        }
        public Student(string id, string first, string last)  //overloaded constructor
        {
            nameFirst = first;
            nameLast = last;
            studentID = id;
            earned = new List<int>();
            possible = new List<int>();
        }

        public void EnterGrade(int earnedValue, int possValue)
        {
            earned.Add(earnedValue);
            possible.Add(possValue);
        }
        public float Average
        {
            get { return average; }
        }
        public string LetterGrade
        {
            get { return letterGrade; }
        }
        public string ID
        {
            get { return studentID; }
            set { studentID = value; }
        }
        //accessor mutators //properties
        public string NameFirst
        {
            get  //accessor - getters
            {
                return nameFirst;
            }
            set  //mutator - setter
            {
                nameFirst = value;
            }
        }
        public string NameLast
        {
            get  //accessor - getters
            {
                return nameLast;
            }
            set  //mutator - setter
            {
                nameLast = value;
            }
        }


        public void CalGrade()
        {
            int totalEarned = 0;
            int totalPossible = 0;

            foreach (int item in earned)
                totalEarned += item;

            foreach (int item in possible)
                totalPossible += item;

            average = (float)totalEarned / totalPossible;  //you could think that this is stored in class Level but not.
            average = (float)Math.Round(average, 2);


            if (average >= .90)
                letterGrade = "A";

            if (average >= .80 && average < .90)
                letterGrade = "B";

            if (average >= .70 && average < .80)
                letterGrade = "C";

            if (average >= .60 && average < .70)
                letterGrade = "D";

            if (average < .60)
                letterGrade = "U";

        }
    }
}

编辑:我更改了以下代码,现在没有打印任何内容。但是错误似乎消失了。使用调试器,它不会拉数据库。

while (theStudentList.Count != null)
        {
            theStudentList.Add(new Student(fileName));
            //temp = fileName.Split(',');

            for (int x = 0; x < ListLength; x++)
            {

                theStudentList.ElementAt(index).ID = temp[0];
                theStudentList.ElementAt(index).NameLast = temp[1];
                theStudentList.ElementAt(index).NameFirst = temp[2];

            }

            index++;
            fileName = filStream.ReadLine();

        }

        s.CalGrade();

        return path;

System.ArgumentOutOfRangeException:'索引超出范围。必须为非负数并且小于集合的大小。 参数名称:index'

什么都不会打印到控制台。 这是txt文件; 0199911,Bill,Gates,27,30,56,60,0,30,83,100,57,60,0,30,59,60,0,30,59,60,88,100 0199912,史蒂夫,乔布斯,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100 0199913,马克·安德烈森,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100 0199914,Larry,Ellisen,30,30,55,60,25,30,70,100,55,60,25,30,50,60,0,30,58,60,80,100

0 个答案:

没有答案