C#基础(16)实践:学生成绩管理系统
简介
通过基础部分的学习,我们已经能进行一些实际应用的开发,学生成绩系统我相信是大家基本在大学期间上程序课必定会经历的一个小项目。
这个小项目看上去简单,但是思考量却不少。
这里就不带着大家一步一步讲解了,因为里面涉及到一些博主没有讲到过的库的使用。
博主尽量写了多的注释来帮助你理解。
如果你有所不懂,建议多查多思考多问。
你也可以自己尝试去实现一下这样的系统:
- 能记录学生姓名和各科成绩(集合属性,使用结构体),并且不能重复录入(姓名检重)
- 查看平均成绩排名(排序算法)
- 开始界面功能选择(Switch)
- 删除相关信息(检索)
- 记录和写入(文件操作——格外内容,需要自己动手搜寻资料学习)
你可以先自己尝试,然后再来看博主给出的代码学习思路。当然可能有些地方博主用了其他的处理,你可以在博主的基础上,用自己的想法去实现。
请务必多敲代码,这样才能让你在代码路上走得更远。
// 引用所需的命名空间
using System;
using System.IO; // 用于文件操作,如读取和写入文件
using System.Linq; // 提供LINQ功能,用于查询和操作集合
using System.Collections.Generic; // 用于使用泛型集合类,如 List class Program
{private const string FilePath = "grades.txt"; // 定义存储成绩数据的文件路径 static void Main(string[] args){while (true) // 无限循环,直到用户选择退出 {Console.WriteLine("请选择操作: 1. 录入成绩 2. 查看成绩 3. 删除成绩 4. 退出");string choice = Console.ReadLine();switch (choice){case "1":InputGrades(); // 调用输入成绩的方法 break;case "2":DisplayGrades(); // 调用显示成绩的方法 break;case "3":DeleteGrades(); // 调用删除成绩的方法 break;case "4":return; // 退出程序 default:Console.WriteLine("无效的选择,请重新输入。");break;}}}// 方法: 输入学生成绩 private static void InputGrades(){Console.Write("请输入学生姓名: ");string name = Console.ReadLine();// 检查学生是否存在 if (IsStudentExists(name)){Console.WriteLine("该学生已存在,请重新输入。");return;}// 提示用户输入成绩 Console.Write("请输入数学成绩: ");decimal math = decimal.Parse(Console.ReadLine());Console.Write("请输入语文成绩: ");decimal chinese = decimal.Parse(Console.ReadLine());Console.Write("请输入英语成绩: ");decimal english = decimal.Parse(Console.ReadLine());// 组织录入的成绩字符串 string record = $"{name},{math},{chinese},{english}";// 将成绩追加到文件中 File.AppendAllText(FilePath, record + Environment.NewLine);Console.WriteLine("成绩录入成功!");}// 方法: 检查学生是否已存在 private static bool IsStudentExists(string name){// 检查文件是否存在 if (!File.Exists(FilePath)) return false;// 读取文件中的所有记录,并使用LINQ检查学生是否存在 string[] records = File.ReadAllLines(FilePath);return records.Any(record => record.StartsWith(name + ",")); // 返回是否找到匹配的学生 }// 方法: 显示所有录入的成绩 private static void DisplayGrades(){// 检查文件是否存在 if (!File.Exists(FilePath)){Console.WriteLine("没有成绩记录。");return;}// 读取所有记录 string[] records = File.ReadAllLines(FilePath);List<Student> students = new List<Student>(); // 创建学生列表 // 遍历每一条记录并解析为学生对象 foreach (string record in records){string[] parts = record.Split(','); // 按逗号分隔记录 if (parts.Length == 4) // 预期为四个部分 {// 尝试解析成绩,如果失败则报告错误 if (decimal.TryParse(parts[1], out decimal math) &&decimal.TryParse(parts[2], out decimal chinese) &&decimal.TryParse(parts[3], out decimal english)){students.Add(new Student{Name = parts[0],MathScore = math,ChineseScore = chinese,EnglishScore = english});}else{Console.WriteLine($"解析记录失败: {record}");}}}// 按照平均成绩降序排序学生 var sortedStudents = students.OrderByDescending(s => s.AverageScore()).ToList();Console.WriteLine("已录入的成绩(按平均成绩排序):");// 显示所有学生的成绩信息 foreach (var student in sortedStudents){Console.WriteLine($"姓名: {student.Name}, 数学: {student.MathScore}, 语文: {student.ChineseScore}, 英语: {student.EnglishScore}, 平均成绩: {student.AverageScore()}");}}// 方法: 删除学生成绩 private static void DeleteGrades(){Console.Write("请输入要删除的学生姓名: ");string name = Console.ReadLine();// 检查学生是否存在 if (!IsStudentExists(name)){Console.WriteLine("未找到该学生的记录。");return;}try{// 读取所有记录并过滤掉要删除的记录 string[] records = File.ReadAllLines(FilePath);var updatedRecords = records.Where(record => !record.StartsWith(name + ",")).ToList();// 将更新后的记录写回文件 File.WriteAllLines(FilePath, updatedRecords);Console.WriteLine("成绩删除成功!");}catch (Exception ex){// 捕获写入文件时的异常并提示用户 Console.WriteLine("删除失败: " + ex.Message);}}
}// 定义学生类
class Student
{public string Name { get; set; } // 学生姓名 public decimal MathScore { get; set; } // 数学成绩 public decimal ChineseScore { get; set; } // 语文成绩 public decimal EnglishScore { get; set; } // 英语成绩 // 方法: 计算平均成绩 public decimal AverageScore(){return (MathScore + ChineseScore + EnglishScore) / 3; // 计算平均成绩 }
}