C# 学生成绩管理系统:计算 GPA 并排序
以下是使用 C# 设计的学生成绩管理程序的示例代码:\n\ncsharp\nusing System;\nusing System.Collections.Generic;\n\nnamespace StudentGradeManagement\n{\n class Program\n {\n static void Main(string[] args)\n {\n List<Student> students = new List<Student>();\n\n // 输入学生信息\n Console.WriteLine("请输入学生信息:");\n while (true)\n {\n Console.WriteLine("学号:");\n string id = Console.ReadLine();\n Console.WriteLine("姓名:");\n string name = Console.ReadLine();\n Console.WriteLine("课程名:");\n string courseName = Console.ReadLine();\n\n // 输入课程学分和成绩\n Console.WriteLine("课程学分:");\n double credit = Convert.ToDouble(Console.ReadLine());\n Console.WriteLine("课程成绩:");\n double score = Convert.ToDouble(Console.ReadLine());\n\n // 计算GPA\n double gpa = CalculateGPA(credit, score);\n\n // 添加学生到列表\n students.Add(new Student(id, name, courseName, credit, score, gpa));\n\n Console.WriteLine("是否继续输入学生信息?(Y/N)");\n string input = Console.ReadLine();\n if (input.ToUpper() != "Y")\n {\n break;\n }\n }\n\n // 按GPA排序学生\n students.Sort();\n\n // 输出学生信息\n Console.WriteLine("\n学生成绩单:");\n Console.WriteLine("{0,-10}{1,-10}{2,-15}{3,-10}{4,-10}{5,-10}", "学号", "姓名", "课程名", "学分", "成绩", "GPA");\n foreach (var student in students)\n {\n Console.WriteLine("{0,-10}{1,-10}{2,-15}{3,-10}{4,-10}{5,-10}", student.Id, student.Name, student.CourseName, student.Credit, student.Score, student.GPA);\n }\n }\n\n static double CalculateGPA(double credit, double score)\n {\n if (score >= 90)\n {\n return 4.0 * credit;\n }\n else if (score >= 85)\n {\n return 3.7 * credit;\n }\n else if (score >= 82)\n {\n return 3.3 * credit;\n }\n else if (score >= 78)\n {\n return 3.0 * credit;\n }\n else if (score >= 75)\n {\n return 2.7 * credit;\n }\n else if (score >= 72)\n {\n return 2.3 * credit;\n }\n else if (score >= 68)\n {\n return 2.0 * credit;\n }\n else if (score >= 64)\n {\n return 1.5 * credit;\n }\n else if (score >= 60)\n {\n return 1.0 * credit;\n }\n else\n {\n return 0.0;\n }\n }\n }\n\n class Student : IComparable<Student>\n {\n public string Id { get; }\n public string Name { get; }\n public string CourseName { get; }\n public double Credit { get; }\n public double Score { get; }\n public double GPA { get; }\n\n public Student(string id, string name, string courseName, double credit, double score, double gpa)\n {\n Id = id;\n Name = name;\n CourseName = courseName;\n Credit = credit;\n Score = score;\n GPA = gpa;\n }\n\n public int CompareTo(Student other)\n {\n return other.GPA.CompareTo(GPA); // 按GPA降序排序\n }\n }\n}\n\n\n这个程序使用了一个Student类来表示学生对象,并实现了IComparable<Student>接口以便可以对学生进行排序。在Main方法中,通过循环让用户输入学生信息,并根据输入的课程学分和成绩计算GPA,然后将学生对象添加到students列表中。最后,对学生列表按照GPA进行降序排序,并将学生的信息输出到控制台上。
原文地址: http://www.cveoy.top/t/topic/pMtU 著作权归作者所有。请勿转载和采集!