简体   繁体   English

如何计算学生的平均绩点(GPA)

[英]How to calculate the grade point average(GPA) of students

I want to write a method for a University class that takes a discipline as a parameter and returns the grade point average(GPA) of the students in that discipline, but I don't know how to do it, this is the signature of the method:我想为大学课程编写一个以学科为参数并返回该学科学生的平均绩点(GPA)的方法,但我不知道该怎么做,这是方法:

public double AverageOfDiscipline(discipline D)
    {
        double sum = 0;
        for (int i = 0; i < students.Length; i++)
        {
            //???
        }
        return //????
    }

and this is my project:这是我的项目:

public enum discipline { Computer, Civil, Mechanical, Electrical}
public enum educationType { Undergraduate, Postgraduate}
class Student
{
    public string nameAndLastName;
    public double GPA;
    public discipline discipline;
    public educationType education;
    public Student(string nameAndLastName, double GPA, discipline discipline, educationType education)
    {
        this.nameAndLastName = nameAndLastName;
        this. GPA = GPA;
        this.discipline = discipline;
        this.education = education;
    }
    public string ToString()
    {
        return nameAndLastName + ": "+ education + ", "+ discipline+ "; "+"GPA: "+ GPA ;
    }
}

class University
{
    public string uniName;
    public Student[] students;
    public double AverageOfDiscipline(discipline D)
    {
        double sum = 0;
        for (int i = 0; i < students.Length; i++)
        {
            //???
        }
        return //????
    }
}

Thanks for your help.谢谢你的帮助。

For the university class you need to iterate through the students array, check if they are pursuing the specified displine, sum then calculare average as below对于大学课程,您需要遍历学生数组,检查他们是否正在追求指定的学科,然后求和然后计算平均值如下

  public double AverageOfDiscipline(discipline D)
        {
            double sum = 0;
            int totalStudentsForDispline = 0;
            for (int i = 0; i < students.Length; i++)
            {
                if (students[i].discipline == D)
                {
                    sum += students[i].GPA;
                    totalStudentsForDispline += 1;
                }
            }

            return sum > 0 ? sum / totalStudentsForDispline : 0;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM