C# Challenge 12 - 引導式專案 - 開發 Foreach 和 if-elseif-else 結構,以在 C# 中處理陣列資料

C# Challenge 12 - 引導式專案 - 開發 Foreach 和 if-elseif-else 結構,以在 C# 中處理陣列資料

Microsoft 2023 年所提供的 C# codecamp 基礎課程,總共有 38 個單元,完成後就可以獲得 Certification ,今天要來跟大家分享的是第 12 單元的內容。

本節內容

學生評分應用程式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// initialize variables - graded assignments
int examAssignments = 5;

int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 };
int[] andrewScores = new int[] { 92, 89, 81, 96, 90, 89 };
int[] emmaScores = new int[] { 90, 85, 87, 98, 68, 89, 89, 89 };
int[] loganScores = new int[] { 90, 95, 87, 88, 96, 96 };

// Student names
string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" };

int[] studentScores = new int[10];

string currentStudentLetterGrade = "";

// Write the Report Header to the console
Console.WriteLine("Student\t\tGrade\n");

foreach (string name in studentNames)
{
string currentStudent = name;

if (currentStudent == "Sophia")
studentScores = sophiaScores;

else if (currentStudent == "Andrew")
studentScores = andrewScores;

else if (currentStudent == "Emma")
studentScores = emmaScores;

else if (currentStudent == "Logan")
studentScores = loganScores;

// initialize/reset the sum of scored assignments
int sumAssignmentScores = 0;

// initialize/reset the calculated average of exam + extra credit scores
decimal currentStudentGrade = 0;

// initialize/reset a counter for the number of assignment
int gradedAssignments = 0;

// loop through the scores array and complete calculations for currentStudent
foreach (int score in studentScores)
{
// increment the assignment counter
gradedAssignments += 1;

if (gradedAssignments <= examAssignments)
// add the exam score to the sum
sumAssignmentScores += score;

else
// add the extra credit points to the sum - bonus points equal to 10% of an exam score
sumAssignmentScores += score / 10;
}

currentStudentGrade = (decimal)(sumAssignmentScores) / examAssignments;

if (currentStudentGrade >= 97)
currentStudentLetterGrade = "A+";

else if (currentStudentGrade >= 93)
currentStudentLetterGrade = "A";

else if (currentStudentGrade >= 90)
currentStudentLetterGrade = "A-";

else if (currentStudentGrade >= 87)
currentStudentLetterGrade = "B+";

else if (currentStudentGrade >= 83)
currentStudentLetterGrade = "B";

else if (currentStudentGrade >= 80)
currentStudentLetterGrade = "B-";

else if (currentStudentGrade >= 77)
currentStudentLetterGrade = "C+";

else if (currentStudentGrade >= 73)
currentStudentLetterGrade = "C";

else if (currentStudentGrade >= 70)
currentStudentLetterGrade = "C-";

else if (currentStudentGrade >= 67)
currentStudentLetterGrade = "D+";

else if (currentStudentGrade >= 63)
currentStudentLetterGrade = "D";

else if (currentStudentGrade >= 60)
currentStudentLetterGrade = "D-";

else
currentStudentLetterGrade = "F";

//Console.WriteLine("Student\t\tGrade\tLetter Grade\n");
Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}");
}

// required for running in VS Code (keeps the Output windows open to view results)
Console.WriteLine("\n\rPress the Enter key to continue");
Console.ReadLine();

重點整理

也是一個 foreach 的練習專案,內容大同小異不過有結合上一單元的一些知識,一樣直接把答案貼上來給大家參考。
那我們下次見ʘ‿ʘ


參考資料

評論