Looping with for, foreach, do, while

jason.zhu 2021-02-19 03:13:57 +00:00
parent 3894e9370f
commit 93db36a149
1 changed files with 14 additions and 5 deletions

View File

@ -26,12 +26,21 @@ namespace GradeBook
result.highGrade = double.MinValue; result.highGrade = double.MinValue;
result.lowGrade = double.MaxValue; result.lowGrade = double.MaxValue;
foreach (var grade in this.grades) // foreach (var grade in this.grades)
// {
// result.lowGrade = Math.Min(grade, result.lowGrade);
// result.highGrade = Math.Max(grade, result.highGrade);
// result.Average += grade;
// }
var index = 0;
do
{ {
result.lowGrade = Math.Min(grade, result.lowGrade); result.lowGrade = Math.Min(grades[index], result.lowGrade);
result.highGrade = Math.Max(grade, result.highGrade); result.highGrade = Math.Max(grades[index], result.highGrade);
result.Average += grade; result.Average += grades[index];
} index += 1;
} while (index < grades.Count);
result.Average /= grades.Count; result.Average /= grades.Count;
return result; return result;