添加3个类,分别实现 IComparer接口,实现对Student类的三个字段的排序。
1、学生类:学号、姓名、年龄
2、请选择:1、添加学生信息。2、删除学生信息 2、查询学生信息。
3、重复的学号不能添加。
4、查询学生信息功能中有:1、查询所有(按学号排序)2、查询所有(按姓名排序),2、查询所有(按年龄排序)4、按学号查询(查没有,则打印查无此学生)5、退出

学生类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _01
{class Student{public string Num { get; set; }//学号public string Name { get; set; }//姓名public int Age { get; set; }//年龄//创建带参的构造方法public Student(string num,string name,int age) {this.Num = num;this.Name = name;this.Age = age;}//创建无参的构造方法,在本次代码中未使用public Student() { }//重写了Tostring()的方法将字典中的值输出打印public override string ToString(){return $"学号:{Num}姓名:{Name}年龄:{Age}";}//创建比较器用于比较public int CompareTo(Student other){return this.Num.CompareTo(other.Num);}}
}

工具类(Utility)

工具类中封装了一些方法用于调用,使得主方法中的代码简洁

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _01
{//判断输入的数字是否正确class Utility{public static int readMenuSelection() {int c;for (; ; ) {c = int.Parse(Console.ReadLine()); if (c != 1 && c != 2 && c != 3 && c != 4) {Console.WriteLine("选择错误,请重新输入:"); } else break;}return c;}public static int readMenuSelection2(){int c;for (; ; ){c = int.Parse(Console.ReadLine());if (c != 1 && c != 2 && c != 3 && c != 4&&c!=5){Console.WriteLine("选择错误,请重新输入:");}else break;}return c;}//用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。public static string readConfirmSelection(){string b;for (; ; ){b = Console.ReadLine();//将输入的值转换成小写的字母,用于用户区分大小写的输入string c = b.ToLowerInvariant();if (c.Equals("y")||c.Equals("n")){break;}else{Console.WriteLine("选择错误,请重新输入:"); }}return b;}//创建添加的方法public static void AddStudent(Dictionary<string, Student> dc){bool Flag = true;while (Flag){Console.WriteLine("请输入学号:");string num = Console.ReadLine();Console.WriteLine("请输入姓名:");string name = Console.ReadLine();Console.WriteLine("请输入年龄:");int age = int.Parse(Console.ReadLine());Student student = new Student(num, name, age);//判断输入的学号是否重复if (!dc.ContainsKey(num)){dc.Add(student.Num, student);Console.WriteLine("添加成功");}else{Console.WriteLine("已存在,添加失败");}Console.WriteLine("是否继续添加(Y/N)):");string b = Utility.readConfirmSelection();if (b.Equals("n")){Flag = false;}}}//创建一个输出全部信息的方法public static void Print(Dictionary<string,Student> dc) {//循环遍历,将 Dictionary<K,V> 类中的值赋值给item//需要重写Tostring方法,否则输出的值为该值得命名空间foreach (var item in dc.Values){Console.WriteLine(item);}}//删除学生信息public static void DeleteStudent(Dictionary<string, Student> dc) {Console.WriteLine("请输入要删除的学生学号");string num = Console.ReadLine();//判断num值是否在该类中if (dc.ContainsKey(num)){//根据提供的num移除目标dc.Remove(num);Console.WriteLine("删除成功");}else{Console.WriteLine("该学号的学生信息不存在");}}//将排序后的元素输出internal static void Print(List<Student> students){foreach (var item in students){Console.WriteLine(item);}}//按照学号查询,如果没查到返回查无此人public static void QueryByNum(Dictionary<string, Student> dc){Console.WriteLine("请输入你要查询的学号");string num = Console.ReadLine();//if (dc.ContainsKey(num)){Console.WriteLine(dc[num]);}else{Console.WriteLine("查无此人");}}//按年龄排序public static void Age(Dictionary<string, Student> dc){List<Student> students = new List<Student>();//字典中无序,将字典中的值存放到有序的list集合中students.AddRange(dc.Values);//调用NameSort的方法students.Sort(new AgeSort());Utility.Print(students);}//按名字排序public static void NameSort(Dictionary<string, Student> dc){List<Student> students = new List<Student>();//字典中无序,将字典中的值存放到有序的list集合中students.AddRange(dc.Values);//调用NameSort的方法students.Sort(new NameSort());Utility.Print(students);}//按学号排序public static void NumSort(Dictionary<string, Student> dc){List<Student> students = new List<Student>();//字典中无序,将字典中的值存放到有序的list集合中students.AddRange(dc.Values);//调用NameSort的方法students.Sort(new NumSort());Utility.Print(students);}}
}

比较器

创建三个比较器用于比较排序,使用IComparer<>接口

  1. 年龄比较器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _01
{class NameSort : IComparer<Student>{public int Compare(Student x, Student y){return x.Name.CompareTo(y.Name);}}
}

2.姓名比较器`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _01
{class NameSort : IComparer<Student>{public int Compare(Student x, Student y){return x.Name.CompareTo(y.Name);}}
}

3.学号比较器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _01
{//构造比较器,进行比较class NumSort : IComparer<Student>{public int Compare(Student x, Student y){return x.Num.CompareTo(y.Num);}}
}

主方法中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _01
{class Program{static void Main(string[] args){Student stu1 = new Student("007","李华",12);Student stu2 = new Student("004", "张三", 13);//Dictionary<数据类型,数据类型> 对象名 = new Dictionary<数据类型,数据类型>();Dictionary<string, Student> ha = new Dictionary<string, Student>();//将学生类对象添加到字典中ha.Add(stu1.Num,stu1);ha.Add(stu2.Num,stu2);bool Flag = true;while (Flag){Console.WriteLine("请选择:1、添加学生信息。2、删除学生信息 3、查询学生信息;4.退出");int a = Utility.readMenuSelection();switch (a){case 1://添加Utility.AddStudent(ha);//输出Utility.Print(ha);break;case 2://删除Utility.DeleteStudent(ha);Utility.Print(ha);break;case 3://查询Console.WriteLine("1、查询所有(按学号排序)2、查询所有(按姓名排序),3、查询所有(按年龄排序)" +"4、按学号查询(查没有,则打印查无此学生)5、退出");int q = Utility.readMenuSelection2();switch (q){case 1://查询所有(按学号排序)Utility.NumSort(ha);break;case 2://查询所有(按姓名排序)Utility.NameSort(ha);break;case 3://查询所有(按年龄排序)Utility.Age(ha);break;case 4://按学号查询(查没有,则打印查无此学生)Utility.QueryByNum(ha);break;case 5://退出break;default:break;}break;case 4:Console.WriteLine("确认是否退出(Y/N)");string b = Utility.readConfirmSelection();if (b.Equals("y")){Flag = false;}//退出break;default:break;}}Console.ReadKey();}}
}

以上就是用一些简单的代码完成一个简易的学生管理系统,因为是初学所以有一些代码可能写得不是很规范,望提出,谢谢!

C#简易学生管理系统相关推荐

  1. Java09-day09【ArrayList(概述、构造方法、常用方法、遍历)、简易学生管理系统】

    java零基础入门到精通(2019版)[黑马程序员] 视频+资料:[链接:https://pan.baidu.com/s/1MdFNUADVSFf-lVw3SJRvtg   提取码:zjxs] &qu ...

  2. 制作基于springboot的简易学生管理系统(详细)

    制作基于springboot的简易学生管理系统(详细) 基于书本与百度创作,内容简易,请多多指教( ̄▽ ̄)/ 设计一个简易学生管理系统 所需环境 创建一个springboot项目 设计数据库 配置Gr ...

  3. GUI+Mysql 仿照水果超市实现简易学生管理系统

    GUI+Mysql 仿照水果超市实现学生管理系统! **当我们学习完到GUI界面和JDBC的时候,就可以实现一些简单的小程序的. 下面是一个仿照水果超市实现学生管理系统的介绍,由一个主类实现.** 首 ...

  4. c语言实现简易学生管理系统

    1.内容用到结构体,链表,函数,文件操作 2.该代码实现了增删改查,不过缺点,没有进行链表的内存释放会造成内存泄漏,写还是挺简单的,不过懒得写了 直接上代码自己感悟: 代码: #include< ...

  5. Oracle PL/SQL 实现简易学生管理系统

    写在前面 写着练手的,若有错,欢迎大家指正! 开始之前准备 数据库:oracle 工具:Oracle SQL Developer(cmd也可操作,大家随意) 1.设计目标 1. 用户注册:注册时检查名 ...

  6. 学习项目-plsql实现简易学生管理系统

    要求: 使用plsql完成一个简单的系统设计开发. 详细信息: 学生表:学号.姓名.年龄.性别.年级.入学日期. 教师表:教师号.姓名.年龄. 选课表:学号.课程号.课程名.教师号.课程学分. 成绩表 ...

  7. python学生管理系统-学生管理系统python

    广告关闭 腾讯云+校园是针对学生用户推出的专项扶持计划,1核2G云服务器9元/月起,云数据库2元/月起,并享受按购买价续费的优惠,助力莘莘学子轻松上云 print(该学生不存在)return none ...

  8. 【C 语言】文件操作 ( 学生管理系统 | 插入数据 | 查询数据 | 删除数据 )

    文章目录 一.学生管理系统 1.插入数据 2.查询数据 3.删除数据 二.完整代码 一.学生管理系统 实现一个简易学生管理系统 , 验证文件操作 ; 1.插入数据 从命令行接收数据 , 放入结构体成员 ...

  9. Python基础day05【函数应用:学生管理系统、拆包、今日总结】

    视频.源码.课件.软件.笔记:超全面Python基础入门教程[十天课程]博客笔记汇总表[黑马程序员] Python基础day05[函数(函数传参的两种形式.函数形参).拆包.引用.可变与不可变类型.引 ...

最新文章

  1. springboot + shiro 验证码与记住登录
  2. 一大波物联网僵尸正在袭来,都有啥安全保护方法?
  3. 深度解密Go语言之sync.pool
  4. 接入amazon avs_每日新闻综述:亚马逊将互联网接入推向全球的宏伟计划
  5. 使用 IntraWeb (39) - THttpRequest、THttpReply
  6. [转] new 和delete
  7. 【Android UI设计与开发】10:滑动菜单栏(二)SlidingMenu 动画效果的实现
  8. Android 5.1 memory leak,Android 性能优化之使用MAT分析内存泄露问题
  9. python删除数据框中的字符串列_如何根据条件删除pandas数据框中的列?
  10. a3967驱动_Arduino A3967 步进电机驱动板 EasyDriver Stepper Motor
  11. Hadoop 原理总结
  12. final 和effectively final区别
  13. 西门子数控系统的机床数据采集
  14. java不同时区时间转换,Java在不同时区转换时间
  15. 光华股份深交所上市:市值51亿 应收账款余额超5亿
  16. RK3568平台开发系列讲解(安卓适配篇)Android11 预安装应用功能
  17. 最优化--等式约束最优性条件
  18. 热门软件看点:QQ和它的对手们 收藏 转载
  19. 强大的Http监控工具Fidder
  20. 搞定这8个实战项目,秒杀80%人工智能工程师面试者

热门文章

  1. 【算法练习】动态规划/搜索/状态压缩 百练poj4124:海贼王之伟大航路
  2. 带你轻松遍历用户生命价值与流失挽救(上):流量下的价值套路
  3. 步进电机的失步和过冲
  4. e2e 模拟用户行为的测试
  5. 商用WiFi如何在三四线城市挣到钱?
  6. 中国生物制药2020年前九个月集团收入约181.3亿元
  7. CSS3只让背景图片旋转180度
  8. 你问我答,这样的学习方式你喜欢吗
  9. 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
  10. 新时期网络间谍活动的现状