boost04 2022-12-10 22:46 采纳率: 50%
浏览 80
已结题

c#成绩排序,求帮助

题目考试成绩排序
要求
1实现从高到低排序
2统计分组人数及百分比(分为大于等于90807060及不及挌)
3分数数据来源于文本文件
4处理结果保存在一个文本文件中

  • 写回答

5条回答 默认 最新

  • 编程乐趣 .Net领域优质创作者 2022-12-10 23:40
    关注

    C#代码如下

    using System;
    using System.IO;
    using System.Linq;
    
    namespace ScoreSorter
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 读取文件中的分数
                var scores = File.ReadAllLines("scores.txt")
                                 .Select(line => int.Parse(line))
                                 .ToList();
    
                // 从高到低排序
                scores.Sort((a, b) => b.CompareTo(a));
    
                // 统计分组人数及百分比
                var scoreGroups = new int[6];
                foreach (var score in scores)
                {
                    if (score >= 90)
                    {
                        scoreGroups[0]++;
                    }
                    else if (score >= 80)
                    {
                        scoreGroups[1]++;
                    }
                    else if (score >= 70)
                    {
                        scoreGroups[2]++;
                    }
                    else if (score >= 60)
                    {
                        scoreGroups[3]++;
                    }
                    else
                    {
                        scoreGroups[4]++;
                    }
                }
    
                // 计算百分比
                var scoreGroupPercents = new double[6];
                for (int i = 0; i < scoreGroups.Length; i++)
                {
                    scoreGroupPercents[i] = (double)scoreGroups[i] / scores.Count;
                }
    
                // 将排序后的分数和分组统计信息写入文件
                using (var writer = File.CreateText("output.txt"))
                {
                    // 写入排序后的分数
                    foreach (var score in scores)
                    {
                        writer.WriteLine(score);
                    }
    
                    // 写入分组人数及百分比
                    writer.WriteLine("90+: " + scoreGroups[0] + " " + scoreGroupPercents[0]);
                    writer.WriteLine("80+: " + scoreGroups[1] + " " + scoreGroupPercents[1]);
                    writer.WriteLine("70+: " + scoreGroups[2] + " " + scoreGroupPercents[2]);
                    writer.WriteLine("60+: " + scoreGroups[3] + " " + scoreGroupPercents[3]);
                    writer.WriteLine("<60: " + scoreGroups[4] + " " + scoreGroupPercents[4]);
                }
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 创建了问题 12月10日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分