Microsoft 2023 年所提供的 C# codecamp 基礎課程,總共有 38 個單元,完成後就可以獲得 Certification ,今天要來跟大家分享的是第六單元章節的內容。
本節內容
計算出某位學生的 GPA 結果,並按照指引一部一部完成數字和字串的處理。
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
| string studentName = "Sophia Johnson"; string course1Name = "English 101"; string course2Name = "Algebra 101"; string course3Name = "Biology 101"; string course4Name = "Computer Science I"; string course5Name = "Psychology 101";
int course1Credit = 3; int course2Credit = 3; int course3Credit = 4; int course4Credit = 4; int course5Credit = 3;
int gradeA = 4; int gradeB = 3;
int course1Grade = gradeA; int course2Grade = gradeB; int course3Grade = gradeB; int course4Grade = gradeB; int course5Grade = gradeA;
int totalCreditHours = 0; totalCreditHours += course1Credit; totalCreditHours += course2Credit; totalCreditHours += course3Credit; totalCreditHours += course4Credit; totalCreditHours += course5Credit;
int totalGradePoints = 0; totalGradePoints += course1Credit * course1Grade; totalGradePoints += course2Credit * course2Grade; totalGradePoints += course3Credit * course3Grade; totalGradePoints += course4Credit * course4Grade; totalGradePoints += course5Credit * course5Grade;
decimal gradePointAverage = (decimal) totalGradePoints/totalCreditHours;
int leadingDigit = (int) gradePointAverage; int trailingDigits = (int) (gradePointAverage * 100) - (leadingDigit * 100);
Console.WriteLine($"Student: {studentName}\n"); Console.WriteLine("Course\t\t\t\tGrade\tCredit Hours");
Console.WriteLine($"{course1Name}\t\t\t{course1Grade}\t\t{course1Credit}"); Console.WriteLine($"{course2Name}\t\t\t{course2Grade}\t\t{course2Credit}"); Console.WriteLine($"{course3Name}\t\t\t{course3Grade}\t\t{course3Credit}"); Console.WriteLine($"{course4Name}\t{course4Grade}\t\t{course4Credit}"); Console.WriteLine($"{course5Name}\t\t{course5Grade}\t\t{course5Credit}");
Console.WriteLine($"\nFinal GPA:\t\t\t{leadingDigit}.{trailingDigits}");
|
重點整理
這也是延續上一個章節到內容,持續的做字串和數字的處理。利用運算符和字串的組合來完成題目的要求,然後針對 decimal
和 int
型別更加多的理解與學習。
知識點的部分有:
- 假設
decimal gradePointAverage = 3.99872831;
。(int) gradePointAverage
的值為何?
Ans : 3
- 在下列程式碼
decimal x = 7 / 5;
中,為什麼 x = 1
?
Ans : 分割兩個整數會執行整數除法
那我們下次見ʘ‿ʘ
參考資料