题目:
设计一个学生的成绩管理系统(学生类、课程类、成绩类)包含以下功能:①登记学生②登记课程③选课④登记成绩⑤排名⑥输出信息
思路:
首先定义是三个类:学生类、课程类、成绩类,再定义一个成绩管理系统类,用来管理上述三个类,然后编写各个选项的具体功能。在定义学生类中,因为后续会对学生进行成绩排名,因此将学生的选课和其成绩存放在了字典里;在选课设计和登记成绩的过程中,通过多次使用if-else语句判断该生是否选过此课程和该课程是否存在的问题;在排名功能中就是简单的排序通过students.OrderBy(s => s.Grades.Values.Average())对学生列表进行排序,计算其成绩字典(Grades)中所有成绩的平均值,然后按照平均值进行排序。再将学生按照排名输出即可;输出功能则使用for-each函数,输出各个学生的各个相关信息。最后主函数中布局页面内容,用Switch函数来选择将要执行的功能,每个分支语句调用该功能具体函数即可。
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StudentScoreManageSystem { // 学生类 class Student { public string Name { get; set; } public string ID { get; set; } public Dictionary<string, int> Grades { get; set; } // key为课程名称,value为成绩 public Student(string name, string id) { Name = name; ID = id; Grades = new Dictionary<string, int>(); } } // 课程类 class Course { public string CourseName { get; set; } public Course(string courseName) { CourseName = courseName; } } // 成绩类 class Grade { public Student Student { get; set; } public Course Course { get; set; } public int Score { get; set; } public Grade(Student student, Course course, int score) { Student = student; Course = course; Score = score; } } // 成绩管理系统类 class GradeManagementSystem { private List<Student> students; private List<Course> courses; private List<Grade> grades; public GradeManagementSystem() { students = new List<Student>(); courses = new List<Course>(); grades = new List<Grade>(); } // 登记学生 public void RegisterStudent(string name, string id) { students.Add(new Student(name, id)); Console.WriteLine("学生登记成功!"); } // 登记课程 public void RegisterCourse(string courseName) { courses.Add(new Course(courseName)); Console.WriteLine("课程登记成功!"); } // 选课 public void SelectCourse(string studentID, string courseName) { Student student = students.Find(s => s.ID == studentID); Course course = courses.Find(c => c.CourseName == courseName); if (student != null && course != null) { // 判断学生是否已选过该课程 if (student.Grades.ContainsKey(courseName)) { Console.WriteLine("学生已经选过该课程!"); } else { student.Grades.Add(courseName, 0); Console.WriteLine("选课成功!"); } } else { Console.WriteLine("学生或课程不存在!"); } } // 登记成绩 public void EnterGrade(string studentID, string courseName, int score) { Student student = students.Find(s => s.ID == studentID); Course course = courses.Find(c => c.CourseName == courseName); if (student != null && course != null) { // 判断学生是否已选过该课程 if (student.Grades.ContainsKey(courseName)) { student.Grades[courseName] = score; grades.Add(new Grade(student, course, score)); Console.WriteLine("成绩登记成功!"); } else { Console.WriteLine("该课程不在学生选择的课程中!"); } } else { Console.WriteLine("学生或课程不存在!"); } } // 排名 public void PrintRanking() { List<Student> rankedStudents = students.OrderBy(s => s.Grades.Values.Average()).ToList(); for (int i = rankedStudents.Count - 1; i >= 0; i--) { Student student = rankedStudents[i]; Console.WriteLine($"排名第{i}的学生:{student.Name},平均成绩:{student.Grades.Values.Average()}"); } } // 输出信息 public void PrintInformation() { Console.WriteLine("学生信息:"); foreach (Student student in students) { Console.WriteLine($"姓名:{student.Name},学号:{student.ID}"); Console.WriteLine("已选课程:"); foreach (string course in student.Grades.Keys) { Console.WriteLine(course); } Console.WriteLine(); } Console.WriteLine("课程信息:"); foreach (Course course in courses) { Console.WriteLine($"课程名称:{course.CourseName}"); } } } class Program { static void Main(string[] args) { GradeManagementSystem system = new GradeManagementSystem(); while (true) { Console.WriteLine("请选择功能:"); Console.WriteLine("1. 登记学生"); Console.WriteLine("2. 登记课程"); Console.WriteLine("3. 选课"); Console.WriteLine("4. 登记成绩"); Console.WriteLine("5. 排名"); Console.WriteLine("6. 输出信息"); Console.WriteLine("0. 退出"); string choice = Console.ReadLine(); switch (choice) { case "1": Console.WriteLine("请输入学生姓名:"); string name = Console.ReadLine(); Console.WriteLine("请输入学生学号:"); string id = Console.ReadLine(); system.RegisterStudent(name, id); break; case "2": Console.WriteLine("请输入课程名称:"); string courseName = Console.ReadLine(); system.RegisterCourse(courseName); break; case "3": Console.WriteLine("请输入学生学号:"); string studentID = Console.ReadLine(); Console.WriteLine("请输入课程名称:"); string selectedCourseName = Console.ReadLine(); system.SelectCourse(studentID, selectedCourseName); break; case "4": Console.WriteLine("请输入学生学号:"); string gradeStudentID = Console.ReadLine(); Console.WriteLine("请输入课程名称:"); string gradeCourseName = Console.ReadLine(); Console.WriteLine("请输入成绩:"); int score = int.Parse(Console.ReadLine()); system.EnterGrade(gradeStudentID, gradeCourseName, score); break; case "5": system.PrintRanking(); break; case "6": system.PrintInformation(); break; case "0": Environment.Exit(0); break; default: Console.WriteLine("无效的选择,请重新输入!"); break; } Console.WriteLine(); } } } }
测试: