添加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. 中国在5G、AI等领域对美国紧追不舍,但设备工艺依旧落后
  2. 几个简单的shell脚本
  3. 【数据竞赛】Kaggle实战之单类别变量特征工程总结!
  4. 【Android】Context
  5. ANN:神经网络堆叠/进化故事( 从感知机到DRBN )
  6. P1428 小鱼比可爱(python3实现)
  7. hbase1.1.1 连接集群_HBase-1.0.1学习笔记(一)集群搭建
  8. 串口通信中ReadFile和WriteFile的超时详解!
  9. datatable插件实现分页功能
  10. 三季度国内光伏市场需求仍将强劲
  11. php中怎么批量修改图片大小,怎么批量修改图片大小 光影魔术手批量处理图片...
  12. 飞鹅WiFi打印机配置,php调用接口
  13. 解决网络栏只剩下飞行模式
  14. 中国鸡荣华鸡为什么干不过洋鸡肯德基
  15. ArcEngine代码 数据导入
  16. 9章 RxJava混合实战
  17. 首届金融行业SD-WAN的应用与实践研讨会上海站
  18. 关于如何理解Glibc堆管理器(Ⅹ——完结、补充、注释——Arena、heap_info、malloc_*)
  19. 色彩敏感度测试 l 据说只有1%的设计师能全对,不服来战!
  20. python-快乐数

热门文章

  1. SQL Server修改列名和列类型
  2. 【uniapp小程序实战】—— 使用腾讯地图获取定位
  3. python数组赋值_对Python中列表和数组的赋值,浅拷贝和深拷贝的实例讲解
  4. 上传文件和导出的测试用例设计
  5. C学生管理系统 指定位置插入学生节点
  6. Python+Appium APP自动化环境搭建
  7. HTML tabindex 属性
  8. 在职人才落户深圳的标准
  9. 20分布式电商项目 - 商家申请入驻
  10. C语言-哈希查找(HASH)-详解(完整代码)