![](/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.