エラー処理
CoddyのC#ジャーニー「ロジックとフロー」セクションの一部。レッスン 45/66。
チャレンジ
簡単潜在的な問題に適切に対処できるよう、堅牢なエラー処理でシステムを強化しましょう。次のメソッドを持つ ErrorHandler という名前のクラスを作成してください。
ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex):入力インデックスを検証し、適切なエラーメッセージを返します。入力が有効な場合は空の文字列を返します。- student インデックスが範囲外の場合は
"Invalid student index"を返します。 - assignment インデックスが範囲外の場合は
"Invalid assignment index"を返します。 - 両方のインデックスが有効な場合は、空の文字列
""を返します。
- student インデックスが範囲外の場合は
SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex):エラー処理を行いながら、安全に score を取得します。- 両方のインデックスが有効な場合は score を返します。
- いずれかのインデックスが無効な場合は
-999を返します。
ProcessBatchUpdate(int[][] scoreGrid, int[][] updates):各 update が[studentIndex, assignmentIndex, score]である一括更新を処理します。- 失敗した各 update に対するエラーメッセージの
string[]配列を返します。 - 各 update は、次のいずれかの具体的なエラーメッセージで失敗する可能性があります。
- update に値がちょうど 3 つ含まれていない場合は、
"Invalid update format"。 - student インデックスが範囲外の場合は、
"Invalid student index"。 - assignment インデックスが範囲外の場合は、
"Invalid assignment index"。 - score が有効な範囲 (0-100) 外の場合は、
"Invalid score value"。
- update に値がちょうど 3 つ含まれていない場合は、
- 各エラーを次の形式にしてください:
"Error at index X: [specific error message]" - すべての update が成功した場合は、空の配列を返します。
- 失敗した各 update に対するエラーメッセージの
string[] として返すには、List<string> errors = new List<string>(); を使用します。List<string> はサイズ変更可能な配列のように機能し、errors.Add(...) を呼び出して項目を追加できます。最後に、errors.ToArray() で変換します。これに必要な using System.Collections.Generic; の import は、ファイルの先頭にすでに含まれています。自分で試してみよう
using System; // この行を削除しないでください
using System.Collections.Generic;
public class ErrorHandler
{
public static string ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex)
{
// ここにコードを書いてください
}
public static int SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex)
{
// ここにコードを書いてください
}
public static string[] ProcessBatchUpdate(int[][] scoreGrid, int[][] updates)
{
// ここにコードを書いてください
}
}
// テストに必要 - 変更しないでください
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;
}
}
public class DataEntry
{
public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
{
// 範囲外のインデックスをチェックする
if (studentIndex < 0 || studentIndex >= scoreGrid.Length ||
assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
{
return -1;
}
// スコアを検証する
if (!DataCollector.ValidateScore(score))
{
return -2;
}
// スコアを設定する
scoreGrid[studentIndex][assignmentIndex] = score;
return 0;
}
public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
{
int successCount = 0;
for (int i = 0; i < studentIndices.Length; i++)
{
int result = SetStudentScore(scoreGrid, studentIndices[i], assignmentIndex, score);
if (result == 0)
{
successCount++;
}
}
return successCount;
}
}
public class DataAnalyzer
{
public static double CalculateStudentAverage(int[][] scoreGrid, int studentIndex)
{
// Check if student index is valid
if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
{
return -1;
}
int sum = 0;
int count = 0;
// 有効なスコアの合計を計算する
for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
{
int score = scoreGrid[studentIndex][j];
if (score != -1) // 未採点の課題を無視する
{
sum += score;
count++;
}
}
// 平均を返す。有効なスコアがない場合は0
return count > 0 ? (double)sum / count : 0;
}
public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
{
// 生徒がいるかどうかをチェックする
if (scoreGrid.Length == 0)
{
return -1;
}
// Check if assignment index is valid
if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
{
return -1;
}
int sum = 0;
int count = 0;
// 課題の有効なスコアの合計を計算する
for (int i = 0; i < scoreGrid.Length; i++)
{
if (assignmentIndex < scoreGrid[i].Length)
{
int score = scoreGrid[i][assignmentIndex];
if (score != -1) // 未採点の課題を無視する
{
sum += score;
count++;
}
}
}
// 平均を返す。有効なスコアがない場合は0
return count > 0 ? (double)sum / count : 0;
}
public static int[] FindHighestScore(int[][] scoreGrid)
{
int highestStudentIndex = 0;
int highestAssignmentIndex = 0;
int highestScore = -1;
// 最高スコアを検索する
for (int i = 0; i < scoreGrid.Length; i++)
{
for (int j = 0; j < scoreGrid[i].Length; j++)
{
int currentScore = scoreGrid[i][j];
if (currentScore > highestScore)
{
highestScore = currentScore;
highestStudentIndex = i;
highestAssignmentIndex = j;
}
}
}
return new int[] { highestStudentIndex, highestAssignmentIndex, highestScore };
}
}ロジックとフローのすべてのレッスン
自分で練習してみよう: C#オンラインコンパイラ