C#实现简单学生信息管理(极简)

才学C#不久,老师布置的;

先建一个.NET Framework控制台项目

在添加上Main要点用的类(Student StudentMange)

主菜单栏:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsoleApp1
  8 {   
  9 
 10    public  class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             bool flag = true;
 15             while (flag == true)
 16             {
 17                 int index = Memu();
 18                 switch (index)
 19                 {
 20                     case 1:
 21                         studentMange.Loda();
 22                         break;
 23                     case 2:
 24                         studentMange.Save();
 25                         break;
 26                     case 3:
 27                         Input();
 28                         break;
 29                     case 4:
 30                         Find();
 31                         break;
 32                     case 5:
 33                         PrintTitle();
 34                         Print();
 35                         break;
 36                     case 0:
 37                         flag = false;
 38                         break;
 39 
 40 
 41                 }
 42                
 43 
 44             }
 45         }
 46         static void Find()
 47         {
 48             bool IsFind = false;
 49             Console.Write("按学生姓名查找:");
 50             string name = Console.ReadLine();
 51             foreach(student s in studentMange.studs)
 52             {
 53                 if (s.Name.Equals(name))
 54                 {
 55                     Console.WriteLine(s);
 56                     IsFind = true;
 57                     break;
 58                 }
 59             }
 60             if (IsFind == false)
 61             {
 62                 Console.WriteLine("没有找到,姓名为[{0}]的学生", name);
 63 
 64             }
 65         }
 66         static void PrintTitle()
 67         {
 68             Console.WriteLine("ID\t\tName\t\tAge");
 69 
 70         }
 71         static void  Print()
 72         {
 73             foreach(student s in studentMange.studs)
 74             {
 75                 s.Show();
 76             }
 77         }
 78         static void Input()
 79         {
 80             Console.WriteLine("请输入学生的相关信息,完成学生信息注册:");
 81             Console.Write("序号:");
 82             int id = int.Parse(Console.ReadLine());
 83             Console.Write("姓名:");
 84             string name = Console.ReadLine();
 85             Console.Write("年龄:");
 86             int age = int.Parse(Console.ReadLine());
 87 
 88             student s = new student { Id = id, Name = name, Age = age };
 89             studentMange.studs.Add(s);
 90         }
 91         static int Memu()
 92         {
 93             Console.WriteLine("\n\n\n=========================");
 94             Console.WriteLine("1.装载学生信息");
 95             Console.WriteLine("2.保存学生信息");
 96             Console.WriteLine("3.输入学生信息");
 97             Console.WriteLine("4.查找学生信息"); 
 98             Console.WriteLine("5.打印学生信息");
 99             Console.WriteLine("0.退出");
100             int iRet = int.Parse(Console.ReadLine());
101             if (iRet < 0 || iRet > 5)
102             {
103                 return Memu();
104             }
105             else
106             {
107                 return iRet;
108             }
109 
110         }
111     }
112 }

 

向Student写入代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp1
 8 {
 9     [Serializable]
10     public  class student
11     {
12         public int Id { get; set; }
13         public string Name { get; set; }
14         public int Age { get; set; }
15         //为了方便输出学生的个人信息,重写ToString方法
16         public override string ToString()
17         {
18             return string.Format("{0}[{1}\n{2}]", Name, Id, Age);
19         }
20         public void Show()
21         {
22             Console.WriteLine("{0}\n{1}\n{2}", Id, Name, Age);
23         }
24     }
25    
26 }

再向StudentMange写入:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 using System.Runtime.Serialization.Formatters.Binary;
 8 
 9 namespace ConsoleApp1
10 {
11    public  class studentMange
12     {
13         public static List<student> studs = new List<student>();
14         public static void Loda()
15         {
16             try
17             {
18                 FileStream fs = new FileStream("stu.dat", FileMode.Open, FileAccess.Read);
19                 BinaryFormatter bf = new BinaryFormatter();
20                 object data = bf.Deserialize(fs);//反序列化操作,得到object类型数据
21                 fs.Close();
22                 studs = (List<student>)data;
23 
24             }
25             catch(Exception ex) { }
26         }
27         public static void Save()
28         {
29             FileStream fs = new FileStream("stu.dat", FileMode.Create, FileAccess.Write);
30             BinaryFormatter bf = new BinaryFormatter();
31             bf.Serialize(fs, studs);
32             fs.Close();
33 
34         }
35     }
36 }

 

posted @ 2021-10-28 11:53  你也喜欢复读机吗?  阅读(259)  评论(0编辑  收藏  举报