演習 - 選択ステートメントを使用してコード ブランチを実装する
この演習では、最終的な数値スコアに基づいて学生のレター の成績を自動的に割り当てるコードを開発し、追加のクレジット プロジェクトスコアが学生の最終成績に組み込まれるようにアプリケーションを更新します。 まず、学生の数値スコアを評価し、文字の成績を割り当てるために使用できる if-elseif-else コンストラクトを記述します。 次に、追加のクレジット作業に関連するアプリケーション要件を調べ、必要なコード更新を行います。 この演習で完了する詳細なタスクは次のとおりです。
学生のスコアを評価してアルファベットの成績を割り当てる
if-elseif-elseコンストラクトを作成します。 評価される式は、学生の数値スコアと、教師が提供する採点グラフから取得したスコアの範囲を比較します。各学生のスコア配列に追加のクレジット スコアを統合し、学生の数値スコアの計算に使用されるコードを更新します。 学生のスコアを合計するために使用される
foreachは、コードを分岐するifステートメントを含むように更新されます。 試験のスコアは、1 つのブランチの合計と、もう一方のブランチの追加のクレジット スコアに適用されます。
Von Bedeutung
この演習を開始する前に、このモジュールの前の演習「配列と foreach ループを作成する」を完了しておく必要があります。
if-elseif-elseコンストラクトを使用してレター グレードを割り当てる
このタスクでは、計算された数値スコアに基づいて文字の成績を割り当てるために使用できる if-elseif-else 構造を開発します。
Program.cs ファイルが Visual Studio Code エディターで開かれていることを確認します。
配列の宣言に使用する行の下に空白のコード行
studentScores作成します。学生のレターの成績を保持するために使用できる文字列変数を作成するには、次のコードを入力します。
string currentStudentLetterGrade = "";Program.cs ファイルの一番下まで下にスクロールします。
計算値を
currentStudentGradeに割り当てる行の下に空白のコード行を追加します。ここで、数値スコアに対応する学業成績を示した、採点グラフを考えてみます。
97 - 100 A+ 93 - 96 A 90 - 92 A- 87 - 89 B+ 83 - 86 B 80 - 82 B- 77 - 79 C+ 73 - 76 C 70 - 72 C- 67 - 69 D+ 63 - 66 D 60 - 62 D- 0 - 59 Fスコアの先頭行 (97 以上の値) には、文字のグレードが "A+" であることに注意してください。 つまり、学生の最終スコアが >= 97 の場合、"A+" という文字の成績が割り当てられます。
学生のスコアが 97 以上の場合に
ifにA+を割り当てるcurrentStudentLetterGradeステートメントを作成するには、次のコードを入力します。if (currentStudentGrade >= 97) currentStudentLetterGrade = "A+";学生のスコアが 93 以上の場合に
else ifにAを割り当てるcurrentStudentLetterGradeステートメントを作成するには、次のコードを入力します。else if (currentStudentGrade >= 93) currentStudentLetterGrade = "A";学生のスコアが97以上の場合、前の
else ifでAが返されたため、currentStudentLetterGradeはtrueをifに割り当てません。レター グレード グラフの行を下に移動すると、この
else ifパターンを拡張できます。 グラフの末尾に達したら、最終的なelseを使用して、60 未満のcurrentStudentGradeをキャッチできます。60 から 92 の範囲のスコアについて、
else ifに学業成績を割り当てるcurrentStudentLetterGradeステートメントを作成します。この手順を完了すると、次のコードに一致する
ifステートメント構造が作成されます。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を追加します。60 未満のスコアに適用される
elseを作成するには、次のコードを入力します。else currentStudentLetterGrade = "F";ここで、アプリケーション コードを確認します。
Program.csコードは、次のコードと一致する必要があります。
// initialize variables - graded assignments int currentAssignments = 5; int[] sophiaScores = new int[] { 90, 86, 87, 98, 100 }; int[] andrewScores = new int[] { 92, 89, 81, 96, 90 }; int[] emmaScores = new int[] { 90, 85, 87, 98, 68 }; int[] loganScores = new int[] { 90, 95, 87, 88, 96 }; // Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan" }; int[] studentScores = new int[10]; string currentStudentLetterGrade = ""; // Display the Report Header Console.WriteLine("Student\t\tGrade\n"); foreach (string name in studentNames) { string currentStudent = name; if (currentStudent == "Sophia") // assign Sophia's scores to the studentScores array studentScores = sophiaScores; else if (currentStudent == "Andrew") // assign Andrew's scores to the studentScores array studentScores = andrewScores; else if (currentStudent == "Emma") // assign Emma's scores to the studentScores array studentScores = emmaScores; else if (currentStudent == "Logan") // assign Logan's scores to the studentScores array 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; foreach (int score in studentScores) { // add the exam score to the sum sumAssignmentScores += score; } currentStudentGrade = (decimal)(sumAssignmentScores) / currentAssignments; 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($"{name}\t\t{currentStudentGrade}\t?"); } Console.WriteLine("Press the Enter key to continue"); Console.ReadLine();アプリケーションは、非常に論理的な上から下の方法で編成されていることに注意してください。
変数を初期化し、アプリケーションのデータ ソースとして機能する配列を作成します。 学生のスコアを提供する配列と、学生の名前を提供する配列があります。 また、
studentScoresという名前の学生に依存しない配列があり、成績の計算時に学生のスコアを保持するために使用できます。成績レポートの列ラベルをコンソールに書き込む
Console.WriteLine()ステートメントがあります。foreach配列を反復処理する外側のstudentNamesループがあり、各学生に対して繰り返されるコード ブロックが提供されます。外側の
foreachループのコード ブロック内で、上から下へのアプローチを使用してコードを整理し続けます。ifなど、現在の学生の名前を評価するif (currentStudent == "Sophia")ステートメントがあります。 式がtrueとして評価されると、学生のスコア配列を学生に依存しない配列に割り当てます。次に例を示します。studentScores = sophiaScores;学生の成績を計算するために必要な 2 つの変数を宣言します。 最初の変数
sumAssignmentScoresは、代入スコアの合計を計算するために使用されます。 2 番目の変数currentStudentGradeは、最終的な数値グレードの計算に使用されます。0の値を使用して変数を初期化します。foreachを反復処理してstudentScoresの値を計算するsumAssignmentScoresループがあります。currentStudentGradeは、sumAssignmentScoresを成績表の課題数で割って計算します。 採点された課題の数は、currentAssignmentsという名前の変数に保持されます。if-elseif-elseの値に基づいて文字による成績評価を割り当てるcurrentStudentGradeコンストラクトがあります。成績レポートを完了するために、学生の名前と成績をコンソールに書き込む
Console.WriteLine()ステートメントがあります。
学生の名前と成績をコンソールに書き込む
Console.WriteLine()ステートメントを見つけます。Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t?");成績レポートには、計算されたレタースコアを含める必要があることを忘れないでください。
成績レポートに
currentStudentLetterGradeの値を含めるには、次のようにコードを更新します。Console.WriteLine($"{currentStudent}\t\t{currentStudentGrade}\t{currentStudentLetterGrade}");Visual Studio Code [ファイル] メニューの [保存] をクリックします。
Visual Studio Code EXPLORER ビューで、[ スタート] を右クリックし、[ 統合ターミナルで開く] を選択します。
[ターミナル] のコマンド プロンプトで「dotnet build」と入力し、Enter キーを押します。
エラー メッセージまたは警告メッセージが表示された場合は、続行する前に修正する必要があります。
ターミナルのコマンド プロンプトで、「 dotnet run 」と入力し、Enter キーを押します。
コードによって次の出力が生成されることを確認します。
Student Grade Sophia 92.2 A- Andrew 89.6 B+ Emma 85.6 B Logan 91.2 A- Press the Enter key to continueアプリケーションが本当に完成に近づいています。 次に、追加のクレジット割り当てを統合する必要があります。
コード ブランチ内に追加のクレジット スコアを統合する
このタスクでは、学生によって提出された追加の評価作業に対応するようにアプリケーションを更新します。 学生は、成績を上げるのに役立つボーナスポイントを獲得するために、追加のクレジットプロジェクトを完了します。 教師は、学生が入力した内容に基づいて、学生ごとに追加のクレジット スコアを提供しています。
- ソフィア:94、90
- Andrew: 89
- エマ:89、89、89
- Logan: 96
これらの追加のクレジット スコアと、教師が提供するアプリケーション要件を使用して、このタスクを完了します。
追加のクレジット割り当てに関連するプロジェクト要件を考慮するには、少し時間がかかります。
このガイド付きプロジェクト モジュールの "準備" ユニットには、次の要件を含む プロジェクトの概要 セクションが含まれています。
アプリケーションでは、追加のクレジット割り当てに対応する必要があります。
- 追加のクレジット スコアは、学生のスコア配列に含まれている必要があります。
- 最終的な数値成績に適用された場合の追加単位の課題は、試験スコアの 10% に相当します。
- 最終的な数値成績を計算する前に、追加の与信課題のスコアを学生の合計試験スコアに追加する必要があります。
学生の最終的な数値評価とレター評価を算出する際に、追加のクレジットスコアを反映させます。
- コードでは、学生のスコア配列内の要素の数に基づいて、追加単位の課題を検出する必要があります。
- コードでは、試験スコアの合計に追加単位スコアを追加する前に、追加単位の課題に 10% の重み付け係数を適用する必要があります。
Program.cs ファイルの一番上までスクロールします。
sophiaScores配列にソフィアの追加のクレジット割り当てスコアを追加するには、次のようにコードを更新します。int[] sophiaScores = new int[] { 90, 86, 87, 98, 100, 94, 90 };配列に含まれるスコアの一覧に、追加のクレジット スコア (
94と90) が追加されていることに注意してください。 シンプル他の学生の追加のクレジット スコアをスコア配列に追加します。
学生のスコア配列が次のコードと一致していることを確認します。
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 };下にスクロールして、割り当てスコアの合計に使用される内部
foreachループを見つけます。foreach (int score in studentScores) { // add the exam score to the sum sumAssignmentScores += score; }実装する必要がある更新プログラムを検討するには、少し時間がかかります。
まず、既に知っていることを考えてみましょう。
-
foreachループは、配列に含まれる要素の数に関係なく、配列のすべての要素を順番に反復処理します。 - 学生には 5 つの試験スコアがあり、関連する変数 (
int currentAssignments = 5;) があることを知っています。 - 配列の末尾に追加のクレジット スコアが含まれていることを知っている。
- 追加単位スコアは試験スコアの 10% に相当することがわかります。
- 学生の最終的な数値成績を計算する前に、追加のクレジット スコアを試験のスコアの合計に追加する必要があることがわかります。
次に、必要なものを検討します。
- スコア配列のどのスコアが追加のクレジット スコアであるかを検出する必要があります。
- 追加のクレジット スコアの値を調整して、試験スコアの 10% に相当するようにする必要があります。
- 学生のスコアを合計するために使用される計算を更新して、合計に追加のクレジット スコアが含まれるようにする必要があります。
-
試験のスコアと追加のクレジット スコアを区別するために必要なコーディングの更新を特定します。
追加のクレジット スコアは、5 つの試験スコアの後に一覧表示されます。 つまり、最初の追加クレジット スコアは、スコア配列の 6 番目のスコアになります。 スコア型と配列要素番号の間のこの関係は、
foreachループ内にカウンターが必要であることを示します。 カウンターの値が試験スコアの数を超えると、現在のスコアが追加のクレジット スコアであることがわかります。試験のスコアと追加のクレジット スコアを区別するために実装する必要がある内容を次に示します。
- 内部
foreachループの上に整数を宣言する必要があります。このループを使用して、採点済みの割り当てをカウントできます。 この変数にはgradedAssignments名前を付けることができます。 -
gradedAssignmentsループ内で1をforeachでインクリメントする必要があります。gradedAssignmentsを初期化して0する場合は、foreachコード ブロックの上部にあるカウンターをインクリメントできます。 - カウンター (
if) が試験成績の数より大きいかどうかを評価するgradedAssignmentsステートメントが必要です。 試験の割り当ての数を保持する変数には、currentAssignmentsという名前が付けられます。 現在では試験の課題に加えて追加課題もあるため、この名前は混乱を招く可能性があります。 変数名をcurrentAssignmentsからexamAssignmentsに変更する必要があります。 この名前の変更が実装されたら、ifを使用して(gradedAssignments <= examAssignments)を評価できます。
- 内部
変数名を
currentAssignmentsからexamAssignmentsに変更します。Von Bedeutung
変数名を変更するときは、アプリケーション内の変数のすべてのインスタンスを確実に更新する必要があります。 この場合、2 つのインスタンスがあります。
Visual Studio Code エディター パネルでは、キーボード ショートカット Control + F を使用して、指定したテキストを検索できます。 Visual Studio Code エディター パネルでは、キーボード ショートカット Control + H を使用して、指定したテキストを検索して置換することもできます。
割り当てスコアの合計に使用する
foreachループの上に空白のコード行を作成します。空白のコード行で、
gradedAssignmentsという名前の整数変数を宣言し、0に初期化するには、次のコードを入力します。// initialize/reset a counter for the number of assignments int gradedAssignments = 0;割り当てスコアの合計に使用される
foreachループのコード ブロックの上部に空白のコード行を作成します。空白のコード行に、
gradedAssignmentsループの各反復で1をforeachだけインクリメントする次のコードを入力します。// increment the assignment counter gradedAssignments += 1;式
ifを評価する(gradedAssignments <= examAssignments)ステートメントを作成するには、次のコードを入力します。if (gradedAssignments <= examAssignments)学生のスコアを合計するために使用される計算に必要なコーディングの更新を特定します。
ifステートメントで(gradedAssignments <= examAssignments)がtrueとして評価されると、スコアは試験のスコアになり、その値を合計に追加できます。 式がtrueとして評価されない場合、スコアは追加のクレジット スコアであり、合計に値を追加する前に 10 で除算する必要があります。if-elseコンストラクトは完璧です。sumAssignmentScores += score;ステートメントがifを(gradedAssignments <= examAssignments)として評価する場合、既存の数式trueが適切な計算であることに注意してください。sumAssignmentScores += score;の下に空白のコード行を作成します。空白のコード行で、
elseコンストラクトのif-else部分を構築するには、「else」と入力し、Enter キーを押します。合計に追加のクレジット スコアを追加する数式を作成するには、次のコードを入力します。
// add the extra credit points to the sum - bonus points equal to 10% of an exam score sumAssignmentScores += score / 10;Visual Studio Code [ファイル] メニューの [保存] をクリックします。
ここで、アプリケーション コードを確認します。
更新されたアプリケーションが次のコードと一致していることを確認します。
// 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();
作業を確認する
このタスクでは、アプリケーションを実行して、コード ロジックが想定どおりに動作していることを確認します。
Program.cs ファイルに変更内容を確実に保存します。
Visual Studio Code EXPLORER ビューで、[ スタート] を右クリックし、[ 統合ターミナルで開く] を選択します。
[ターミナル] のコマンド プロンプトで「dotnet build」と入力し、Enter キーを押します。
エラー メッセージまたは警告メッセージが表示された場合は、続行する前に修正する必要があります。
ターミナルのコマンド プロンプトで、「 dotnet run 」と入力し、Enter キーを押します。
コードによって次の出力が生成されることを確認します。
Student Grade Sophia 95.8 A Andrew 91.2 A- Emma 90.4 A- Logan 93 A Press the Enter key to continue[ターミナル] パネルで、実行中のアプリケーションを停止するには、Enter キーを押します。
[ターミナル] パネルを閉じます。
少し時間を取って、次のプロジェクト要件を検討してください。
- アプリケーションでは、コードへの影響を最小限に抑えながら、学生とスコアの追加をサポートする必要があります。
アプリケーションの重要な要件を見落としていますか?
配列と
foreachループの組み合わせで、完全なコード書き換えを行わずに追加の学生を含めできるかどうかを確認します。Program.cs アプリケーションの一番上までスクロールし、次のように配列を更新します。
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 }; int[] beckyScores = new int[] { 92, 91, 90, 91, 92, 92, 92 }; int[] chrisScores = new int[] { 84, 86, 88, 90, 92, 94, 96, 98 }; int[] ericScores = new int[] { 80, 90, 100, 80, 90, 100, 80, 90 }; int[] gregorScores = new int[] { 91, 91, 91, 91, 91, 91, 91 }; // Student names string[] studentNames = new string[] { "Sophia", "Andrew", "Emma", "Logan", "Becky", "Chris", "Eric", "Gregor" };ループ
foreach名前まで下にスクロールし、次のコード行を見つけます。if (currentStudent == "Sophia") studentScores = sophiaScores; else if (currentStudent == "Andrew") studentScores = andrewScores; else if (currentStudent == "Emma") studentScores = emmaScores; else if (currentStudent == "Logan") studentScores = loganScores;新しい学生を含めるには、選択構造の末尾に次のコードを追加します。
else if (currentStudent == "Becky") studentScores = beckyScores; else if (currentStudent == "Chris") studentScores = chrisScores; else if (currentStudent == "Eric") studentScores = ericScores; else if (currentStudent == "Gregor") studentScores = gregorScores; else continue;Visual Studio Code [ファイル] メニューの [保存] をクリックします。
Visual Studio Code EXPLORER ビューで、[ スタート] を右クリックし、[ 統合ターミナルで開く] を選択します。
[ターミナル] のコマンド プロンプトで「dotnet build」と入力し、Enter キーを押します。
エラー メッセージまたは警告メッセージが表示された場合は、続行する前に修正する必要があります。
ターミナルのコマンド プロンプトで、「 dotnet run 」と入力し、Enter キーを押します。
コードによって次の出力が生成されることを確認します。
Student Grade Sophia 95.8 A Andrew 91.2 A- Emma 90.4 A- Logan 93 A Becky 94.8 A Chris 93.4 A Eric 93.4 A Gregor 94.6 A Press the Enter key to continue[ターミナル] パネルで、実行中のアプリケーションを停止するには、Enter キーを押します。
[ターミナル] パネルを閉じます。
これで、このガイド付きプロジェクトが完了しました。