Menu
Coddy logo textTech

טיפול בשגיאות

חלק מהיחידה לוגיקה וזרימת תוכנית במסלול ה-C# של Coddy. שיעור 45 מתוך 66.

challenge icon

אתגר

קל

בואו נשפר את המערכת שלנו בעזרת טיפול robust בשגיאות, כדי להתמודד עם בעיות אפשריות בצורה חלקה. צרו מחלקה בשם ErrorHandler עם המתודות הבאות:

  1. ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex): מאמתת את אינדקסי הקלט ומחזירה הודעת שגיאה מתאימה או מחרוזת ריקה אם הקלט תקין.
    • מחזירה "Invalid student index" אם אינדקס התלמיד מחוץ לטווח.
    • מחזירה "Invalid assignment index" אם אינדקס המטלה מחוץ לטווח.
    • מחזירה מחרוזת ריקה "" אם שני האינדקסים תקינים.
  2. SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex): מאחזרת ציון בבטחה תוך טיפול בשגיאות.
    • מחזירה את הציון אם שני האינדקסים תקינים.
    • מחזירה -999 אם אחד מהאינדקסים אינו תקין.
  3. ProcessBatchUpdate(int[][] scoreGrid, int[][] updates): מעבדת עדכונים באצווה, כאשר כל עדכון הוא [studentIndex, assignmentIndex, score].
    • מחזירה מערך string[] של הודעות שגיאה עבור כל עדכון שנכשל.
    • כל עדכון עלול להיכשל עם אחת מהודעות השגיאה הספציפיות הבאות:
      • "Invalid update format", אם העדכון אינו מכיל בדיוק 3 ערכים.
      • "Invalid student index": אם אינדקס התלמיד מחוץ לטווח.
      • "Invalid assignment index": אם אינדקס המטלה מחוץ לטווח.
      • "Invalid score value": אם הציון מחוץ לטווח התקין (0-100).
    • עצבו כל שגיאה כך: "Error at index X: [specific error message]"
    • מחזירה מערך ריק אם כל העדכונים הצליחו.
רמז: כדי לאסוף שגיאות באופן דינמי לפני החזרתן כמערך string[], השתמשו ב-List<string> errors = new List<string>();. List<string> פועל כמו מערך שניתן לשנות את גודלו, ואפשר לקרוא בו ל-errors.Add(...) כדי להוסיף פריטים. בסוף, המירו אותו באמצעות errors.ToArray(). הייבוא using System.Collections.Generic; הנדרש לכך כבר מופיע בראש הקובץ.

נסו בעצמכם

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)
    {
        // בדקו אם אינדקס התלמיד תקין
        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;
        }
        
        // בדקו אם אינדקס המטלה תקין
        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# אונליין