Menu
CoddyTech

报告生成

本课属于 Coddy C# 学习旅程的“逻辑与流程”单元。第 44 课,共 66 课。

challenge icon

挑战

中等

现在我们已经有了评分系统,接下来创建一个报告模块,用来生成学生表现报告。创建一个名为 ReportGenerator 的类,包含以下方法:

  1. GenerateStudentReport(int[][] scoreGrid, int studentIndex):返回格式化字符串,报告某个学生的分数、平均分和字母等级。
    • 格式:"Student #X | Average: YY.Y | Grade: Z\nAssignment scores: S1, S2, S3, ...",其中 S1、S2、S3 等是每项作业的数值分数。
    • 对于尚未评分的作业(分数为 -1),显示 "N/A" 而不是分数。
    • 如果学生索引无效,则返回 "Invalid student index"。
  2. GenerateClassSummary(int[][] scoreGrid):返回格式化字符串,总结全班的表现。
    • 格式:"Class Summary\nTotal Students: X\nClass Average: YY.Y\nGrade Distribution: A: #, B: #, C: #, D: #, F: #"
  3. GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex):返回格式化字符串,包含某项作业的统计信息。
    • 格式:"Assignment #X | Average: YY.Y | Completion Rate: Z%"
    • 完成率是指这项作业已评分(不为 -1)的学生所占的百分比。
    • 如果作业索引无效,则返回 "Invalid assignment index"。

亲手试试

using System; // 不要删除这一行
using System.Text;

public class ReportGenerator
{
    public static string GenerateStudentReport(int[][] scoreGrid, int studentIndex)
    {
        // 在这里编写你的代码
        
    }
    
    public static string GenerateClassSummary(int[][] scoreGrid)
    {
        // 在这里编写你的代码
        
    }
    
    public static string GenerateAssignmentReport(int[][] scoreGrid, int assignmentIndex)
    {
        // 在这里编写你的代码
        
    }
}

// 测试需要这部分,不要修改
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 };
    }
}

public class GradingSystem
{
    public static string ConvertToLetterGrade(double score)
    {
        if (score < 0 || score > 100)
        {
            return "N/A";
        }
        else if (score >= 90)
        {
            return "A";
        }
        else if (score >= 80)
        {
            return "B";
        }
        else if (score >= 70)
        {
            return "C";
        }
        else if (score >= 60)
        {
            return "D";
        }
        else
        {
            return "F";
        }
    }
    
    public static string GetStudentGrade(int[][] scoreGrid, int studentIndex)
    {
        double average = DataAnalyzer.CalculateStudentAverage(scoreGrid, studentIndex);
        
        if (average == -1)
        {
            return "N/A";
        }
        
        return ConvertToLetterGrade(average);
    }
    
    public static int[] GetClassDistribution(int[][] scoreGrid)
    {
        int[] distribution = new int[5]; // [A, B, C, D, F]
        
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            string grade = GetStudentGrade(scoreGrid, i);
            
            switch (grade)
            {
                case "A":
                    distribution[0]++;
                    break;
                case "B":
                    distribution[1]++;
                    break;
                case "C":
                    distribution[2]++;
                    break;
                case "D":
                    distribution[3]++;
                    break;
                case "F":
                    distribution[4]++;
                    break;
                // N/A 等级不计入分布统计
            }
        }
        
        return distribution;
    }
}

逻辑与流程中的所有课

自己练习: C# 在线编译器