回复 5楼 qubo1982
你没有连接到txt文档啊??
1、将学生信息抽象成一个类Student
程序代码:
public class Student//新建一个学生类,包含学生的信息 { public string Name { get; set; }//姓名 public string No { get; set; }//学号 public string Sex { get; set; } public int Age { get; set; } public Student(string name, string no , string sex, int age) { this.Name = name; this.Age = age; this.Sex = sex; this.No = no; } public override string ToString() { return "姓名:" + Name + ", 年龄:" + Age + ",性别:" + Sex + ",学号:" + No; } }2、定义一个学生管理类StudentManager,负责对学生的增、删、改、查:
程序代码:
public class StudentManager { private static List<Student> StudentList = null; public static void ReadFromFile() // 从文件中读取数据 { StudentList = new List<Student>(); // 读取并解析学生信息,添加到StudentList中 ... } public static void Save() { // 将StudentList保存到文件中 ... } public static List<Student> GetAllStudents() { if (StudentList == null) ReadFromFile(); return StudentList.ToList(); // 返回StudentList的副本 } public static Student SearchByName(string name) { var stu = StudentList.FirstOrDefault(a => a.Name == name); return stu; } public static Student SearchByNo(string no) { var stu = StudentList.FirstOrDefault(a => a.No == no); return stu; } public static Student DeleteByName(string name) { var stu = SearchByName(name); if (stu != null) StudentList.Remove(stu); return stu; } public static Student DeleteByNo(string no) { var stu = SearchByNo(no); if (stu != null) StudentList.Remove(stu); return stu; } public static void Add(Student stu) { StudentList.Add(stu); } }
其实修改的话可以查找出来后直接对Student进行修改
主程序:
程序代码:
StudentManager.ReadFromFile(); StudentManager.Add(new Student(....)); var stu = StudentManager.SearchByName("张三"); if (stu == null) .... else Console.WriteLine(stu); stu.Age = 20; .... StudentManager.Save();
[ 本帖最后由 zhp223 于 2011-3-31 19:20 编辑 ]