![](/img/trans.png)
[英]How to calculate and display average grade from radio button values in a textbox using a for loop?
[英]How to calculate the grade point average(GPA) of students
我想为大学课程编写一个以学科为参数并返回该学科学生的平均绩点(GPA)的方法,但我不知道该怎么做,这是方法:
public double AverageOfDiscipline(discipline D)
{
double sum = 0;
for (int i = 0; i < students.Length; i++)
{
//???
}
return //????
}
这是我的项目:
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 //????
}
}
谢谢你的帮助。
对于大学课程,您需要遍历学生数组,检查他们是否正在追求指定的学科,然后求和然后计算平均值如下
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.