static void Main(string[] args) { List <Student> students = GetStudents(); List <StudentScore> csScores = GetCSScores(); List <StudentScore> dbScores = GetDBScores(); // 彙總成績 //double csScoresSum = 0.0; //double dbScoresSum = 0.0; //double csScoresAvg = 0.0; //double dbScoresAvg = 0.0; //foreach (StudentScore csScore in csScores) //{ // csScoresSum += csScore.Score; //} //foreach (StudentScore dbScore in dbScores) //{ // dbScoresSum += dbScore.Score; //} //csScoresAvg = csScoresSum / students.Count; //dbScoresAvg = dbScoresSum / students.Count; //Console.WriteLine("C#課程分數加總:{0}, 平均:{1}", csScoresSum, csScoresAvg.ToString("0.00")); //Console.WriteLine("DB課程分數加總:{0}, 平均:{1}", dbScoresSum, dbScoresAvg.ToString("0.00")); // 依學生彙總 List <StudentScoreReport> Reports = new List <StudentScoreReport>(); foreach (Student student in students) { int scoreCount = 0; StudentScoreReport report = new StudentScoreReport(); report.Id = student.Id; report.Name = student.Name; foreach (StudentScore csScore in csScores) { if (csScore.Id == student.Id) { report.ScoreSum += csScore.Score; scoreCount++; break; } } foreach (StudentScore dbScore in dbScores) { if (dbScore.Id == student.Id) { report.ScoreSum += dbScore.Score; scoreCount++; break; } } report.ScoreAvg = report.ScoreSum / scoreCount; Reports.Add(report); } //foreach (StudentScoreReport report in Reports) //{ // Console.WriteLine("學生 {0} 分數加總:{1}, 平均:{2}", // report.Name, report.ScoreSum, report.ScoreAvg.ToString("0.00")); //} IEnumerator <StudentScoreReport> reports = Reports.GetEnumerator(); while (reports.MoveNext()) { Console.WriteLine("學生 {0} 分數加總:{1}, 平均:{2}", reports.Current.Name, reports.Current.ScoreSum, reports.Current.ScoreAvg.ToString("0.00")); } Console.ReadLine(); }
static void Main(string[] args) { List<Student> students = GetStudents(); List<StudentScore> csScores = GetCSScores(); List<StudentScore> dbScores = GetDBScores(); // 彙總成績 //double csScoresSum = 0.0; //double dbScoresSum = 0.0; //double csScoresAvg = 0.0; //double dbScoresAvg = 0.0; //foreach (StudentScore csScore in csScores) //{ // csScoresSum += csScore.Score; //} //foreach (StudentScore dbScore in dbScores) //{ // dbScoresSum += dbScore.Score; //} //csScoresAvg = csScoresSum / students.Count; //dbScoresAvg = dbScoresSum / students.Count; //Console.WriteLine("C#課程分數加總:{0}, 平均:{1}", csScoresSum, csScoresAvg.ToString("0.00")); //Console.WriteLine("DB課程分數加總:{0}, 平均:{1}", dbScoresSum, dbScoresAvg.ToString("0.00")); // 依學生彙總 List<StudentScoreReport> Reports = new List<StudentScoreReport>(); foreach (Student student in students) { int scoreCount = 0; StudentScoreReport report = new StudentScoreReport(); report.Id = student.Id; report.Name = student.Name; foreach (StudentScore csScore in csScores) { if (csScore.Id == student.Id) { report.ScoreSum += csScore.Score; scoreCount++; break; } } foreach (StudentScore dbScore in dbScores) { if (dbScore.Id == student.Id) { report.ScoreSum += dbScore.Score; scoreCount++; break; } } report.ScoreAvg = report.ScoreSum / scoreCount; Reports.Add(report); } //foreach (StudentScoreReport report in Reports) //{ // Console.WriteLine("學生 {0} 分數加總:{1}, 平均:{2}", // report.Name, report.ScoreSum, report.ScoreAvg.ToString("0.00")); //} IEnumerator<StudentScoreReport> reports = Reports.GetEnumerator(); while (reports.MoveNext()) { Console.WriteLine("學生 {0} 分數加總:{1}, 平均:{2}", reports.Current.Name, reports.Current.ScoreSum, reports.Current.ScoreAvg.ToString("0.00")); } Console.ReadLine(); }