Data Entry Logic
CoddyのC#ジャーニー「ロジックとフロー」セクションの一部 — レッスン 41/66。
チャレンジ
簡単データ構造が整いましたので、学生の点数の入力と更新のためのロジックを実装しましょう。
DataEntry という名前のクラスを作成し、次のメソッドを実装してください:
SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score):有効であれば特定の学生と課題の点数を設定します。戻り値:- 成功した場合 0
- いずれかのインデックスが範囲外の場合 -1
- 点数が無効な場合 -2
UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score):同じ課題について複数の学生の点数を更新します。成功した更新点数の数を返します。
注意:点数の有効性を確認するために、DataCollector クラスの ValidateScore メソッドを使用してください。
自分で試してみよう
using System; // この行を削除しないでください
public class DataEntry
{
public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
{
// ここにコードを書いてください
}
public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
{
// ここにコードを書いてください
}
}
// Required for testing - do not modify
public class DataCollector
{
public static int[][] CreateScoreGrid(int students, int assignments)
{
int[][] scoreGrid = new int[students][];
for (int i = 0; i < students; i++)
{
scoreGrid[i] = new int[assignments];
}
return scoreGrid;
}
public static bool ValidateScore(int score)
{
return score >= 0 && score <= 100;
}
public static int[][] PopulateWithDefaultValues(int[][] scoreGrid)
{
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
scoreGrid[i][j] = -1;
}
}
return scoreGrid;
}
}ロジックとフローのすべてのレッスン
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling8Data Analysis System
Data Collection SetupData Entry Logic3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety