Menu
CoddyTech

Logic chấm điểm

Thuộc phần Logic và luồng chương trình trong hành trình C# của Coddy. Bài học 43 trên 66.

challenge icon

Thử thách

Dễ

Bây giờ khi đã có thể phân tích dữ liệu, hãy triển khai hệ thống chấm điểm để gán điểm chữ dựa trên điểm số. Tạo một lớp (class) có tên GradingSystem với các phương thức (method) sau:

  1. ConvertToLetterGrade(double score): Chuyển điểm số thành điểm chữ theo thang điểm sau:
    • A: 90-100
    • B: 80-89
    • C: 70-79
    • D: 60-69
    • F: 0-59
    • Không hợp lệ: Trả về "N/A" nếu điểm số không hợp lệ (âm hoặc > 100)
  2. GetStudentGrade(int[][] scoreGrid, int studentIndex): Tính điểm trung bình của một học sinh và trả về điểm chữ tương ứng. Trả về "N/A" nếu chỉ số học sinh không hợp lệ.
  3. GetClassDistribution(int[][] scoreGrid): Trả về một mảng số nguyên biểu thị số lượng từng điểm chữ trong lớp [A, B, C, D, F]. Dựa trên điểm trung bình của từng học sinh.

Tự làm thử

using System; // Đừng xóa dòng này
public class GradingSystem
{
    public static string ConvertToLetterGrade(double score)
    {
        // Viết code của bạn ở đây
        
    }
    
    public static string GetStudentGrade(int[][] scoreGrid, int studentIndex)
    {
        // Viết code của bạn ở đây
        
    }
    
    public static int[] GetClassDistribution(int[][] scoreGrid)
    {
        // Viết code của bạn ở đây
        
    }
}

// Cần cho việc kiểm thử, đừng sửa đổi
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)
    {
        // Kiểm tra các chỉ số có vượt phạm vi không
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // Kiểm tra điểm có hợp lệ không
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // Gán điểm
        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)
    {
        // Kiểm tra chỉ số học sinh có hợp lệ không
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Tính tổng các điểm hợp lệ
        for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
        {
            int score = scoreGrid[studentIndex][j];
            if (score != -1)  // Bỏ qua các bài tập chưa chấm điểm
            {
                sum += score;
                count++;
            }
        }
        
        // Trả về điểm trung bình, hoặc 0 nếu không có điểm hợp lệ
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // Kiểm tra có học sinh nào không
        if (scoreGrid.Length == 0)
        {
            return -1;
        }
        
        // Kiểm tra chỉ số bài tập có hợp lệ không
        if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Tính tổng các điểm hợp lệ của bài tập
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            if (assignmentIndex < scoreGrid[i].Length)
            {
                int score = scoreGrid[i][assignmentIndex];
                if (score != -1)  // Bỏ qua các bài tập chưa chấm điểm
                {
                    sum += score;
                    count++;
                }
            }
        }
        
        // Trả về điểm trung bình, hoặc 0 nếu không có điểm hợp lệ
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static int[] FindHighestScore(int[][] scoreGrid)
    {
        int highestStudentIndex = 0;
        int highestAssignmentIndex = 0;
        int highestScore = -1;
        
        // Tìm điểm cao nhất
        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 };
    }
}

Tất cả bài học trong Logic và luồng chương trình

Tự luyện tập: Trình biên dịch C# online