用Course数组,用属性获得学生学科并成绩计算学生GPA

namespace Proj6_18
{
    class Student               //学生类
    {
        //学生类的字段成员
        int no;                //学号
        string name;           //姓名
        Course[] course;        //学生所修课程组成的对象数组
        double gpa;           //GPA值
        //学生类的构造函数
        public Student(int no,string name, Course[] course)
        {
            this.no = no;
            this.name = name;
            //this.course = course;
            this.course = new Course[course.Length];
            for (int i = 0; i < this.course.Length; i++)
                this.course[i] = course[i];
        }
        public void ComputeGpa()    //根据课程的学分以及学生成绩计算GPA
        {
            int i,sumc=0; //sumc计算总学分数
            double s, sumgpa = 0;//s为课程成绩换算的点数,sumgpa计算课程的学分*对应的点数之和
            for (i = 0; i < course.Length; i++)
            {
                if (course[i].Score >= 90)
                    s = 4.0;            //
                else if (course[i].Score >= 80)
                    s = 3.0;
                else if (course[i].Score >= 70)
                    s = 2.0;
                else if (course[i].Score >= 60)
                    s = 1.0;
                else
                    s = 0.0;
                sumgpa += course[i].Credits * s;
                sumc += course[i].Credits;
            }
            gpa = sumgpa / sumc;
        }
        public void DispStud()  //输出学生成绩信息
        {
            Console.WriteLine("学号:{0} 姓名:{1}已修课程信息如下:", no, name);
            Console.WriteLine("   课程名\t学分\t分数");
            for (int i = 0; i < course.Length; i++)
                Console.WriteLine("   {0}\t\t{1}\t{2}", course[i].Cname, course[i].Credits, course[i].Score);
            Console.WriteLine("该同学的GPA={0:n}", gpa);
        }
    }
    class Course                            //课程类
    {
        //课程类的字段成员
        string cname;    	                //课程名 
        int credits;      	                //课程学分
        double score;                         //分数
        //课程类的构造函数
        public Course(string cname, int credits, double score)
        {
            this.cname = cname;
            this.credits = credits;
            this.score = score;
        }
        //课程类的属性成员
        public string Cname     	        //属性,课程名可读
        {
            get
            { return cname; }
         }
        public int Credits    		        //属性,课程学分可读
        {
            get
            { return credits; }
        }
        public double Score         //属性,课程成绩可读
        {
            get
            { return score; } 
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Course[] course1 = new Course[] {new Course("英语",4,92),new Course("数学",3,80), 
                new Course("语文",2,98),new Course("历史",6,70),new Course("政治",3,89)};
            Student s1 = new Student(101,"王华",course1);
            s1.ComputeGpa();
            s1.DispStud();

        }
    }
}

上一篇:5.课程表3(course scheduleIII)


下一篇:面向对象之组合,内置参数