You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于现有C#代码实现星号形式的学生成绩分布图表?

实现成绩分布星号图表的解决方案

你找的方向完全没错——通过循环遍历分数区间,再统计每个区间内的成绩数量来生成对应星号就行。这里帮你把代码补全,核心思路是先把所有成绩集中起来,再逐个区间统计匹配的成绩数:

第一步:收集所有学生的成绩

首先得把每个学生的5门成绩汇总到一个集合里,方便后续统一统计。假设你的成绩存在二维数组(比如studentsScores[n,5],n是学生总数)里,先做这一步:

List<int> allScores = new List<int>();
// 遍历所有学生的每一门成绩,加入集合
for (int student = 0; student < n; student++)
{
    for (int scoreIdx = 0; scoreIdx < 5; scoreIdx++)
    {
        allScores.Add(studentsScores[student, scoreIdx]);
    }
}

第二步:遍历区间生成星号图表

接下来处理分数区间,注意100是特殊区间,要单独处理(不然会出现100-110的错误区间):

Console.WriteLine("Overall Grade Distribution:");

// 处理0-99的10分区间
for (int i = 0; i < 100; i += 10)
{
    int lower = i;
    int upper = i + 9;
    // 统计当前区间内的成绩数量
    int scoreCount = 0;
    foreach (int score in allScores)
    {
        if (score >= lower && score <= upper)
        {
            scoreCount++;
        }
    }
    // 生成对应数量的星号
    string stars = new string('*', scoreCount);
    Console.WriteLine($"{lower}-{upper}: {stars}");
}

// 单独处理100分的情况
int hundredCount = allScores.Count(s => s == 100);
Console.WriteLine($"100: {new string('*', hundredCount)}");

额外说明

如果你熟悉LINQ,统计成绩数量的部分可以简化成一行:

int scoreCount = allScores.Count(score => score >= lower && score <= upper);

效果和手动循环计数完全一样,只是写法更简洁。

只要把收集成绩的部分适配你现有代码里的成绩存储结构,这段逻辑就能完美生成你想要的星号分布图表啦。

内容的提问来源于stack exchange,提问作者Justin Robert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:12:15