C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)
本文實(shí)例為大家分享了C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
添加3個(gè)類(lèi),分別實(shí)現(xiàn) IComparer接口,實(shí)現(xiàn)對(duì)Student類(lèi)的三個(gè)字段的排序。
1、學(xué)生類(lèi):學(xué)號(hào)、姓名、年齡
2、請(qǐng)選擇:1、添加學(xué)生信息。2、刪除學(xué)生信息 2、查詢(xún)學(xué)生信息。
3、重復(fù)的學(xué)號(hào)不能添加。
4、查詢(xún)學(xué)生信息功能中有:1、查詢(xún)所有(按學(xué)號(hào)排序)2、查詢(xún)所有(按姓名排序),2、查詢(xún)所有(按年齡排序)4、按學(xué)號(hào)查詢(xún)(查沒(méi)有,則打印查無(wú)此學(xué)生)5、退出
學(xué)生類(lèi)
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; }//學(xué)號(hào) ? ? ? ? public string Name { get; set; }//姓名 ? ? ? ? public int Age { get; set; }//年齡 ?? ??? ?//創(chuàng)建帶參的構(gòu)造方法 ? ? ? ? public Student(string num,string name,int age) { ? ? ? ? ? ? this.Num = num; ? ? ? ? ? ? this.Name = name; ? ? ? ? ? ? this.Age = age; ? ? ? ? } ?? ??? ?//創(chuàng)建無(wú)參的構(gòu)造方法,在本次代碼中未使用 ? ? ? ? public Student() { } ? ? ? ? //重寫(xiě)了Tostring()的方法將字典中的值輸出打印 ? ? ? ? public override string ToString() ? ? ? ? { ? ? ? ? ? ? return $"學(xué)號(hào):{Num}姓名:{Name}年齡:{Age}"; ? ? ? ? } ?? ??? ?//創(chuàng)建比較器用于比較 ? ? ? ? public int CompareTo(Student other) ? ? ? ? { ? ? ? ? ? ? return this.Num.CompareTo(other.Num); ? ? ? ? } ? ? } }
工具類(lèi)(Utility)
工具類(lèi)中封裝了一些方法用于調(diào)用,使得主方法中的代碼簡(jiǎn)潔
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _01 { ? ? //判斷輸入的數(shù)字是否正確 ? ? 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("選擇錯(cuò)誤,請(qǐng)重新輸入:");? ? ? ? ? ? ? } 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("選擇錯(cuò)誤,請(qǐng)重新輸入:"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else break; ? ? ? ? ? ? } ? ? ? ? ? ? return c; ? ? ? ? } ? ? ? ? //用于確認(rèn)選擇的輸入。該方法從鍵盤(pán)讀取‘Y'或'N',并將其作為方法的返回值。 ? ? ? ? public static string readConfirmSelection() ? ? ? ? { ? ? ? ? ? ? string b; ? ? ? ? ? ? for (; ; ) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? b = Console.ReadLine(); ? ? ? ? ? ? ? ? //將輸入的值轉(zhuǎn)換成小寫(xiě)的字母,用于用戶(hù)區(qū)分大小寫(xiě)的輸入 ? ? ? ? ? ? ? ? string c = b.ToLowerInvariant(); ? ? ? ? ? ? ? ? if (c.Equals("y")||c.Equals("n")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.WriteLine("選擇錯(cuò)誤,請(qǐng)重新輸入:");? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return b; ? ? ? ? } ? ? ? ? //創(chuàng)建添加的方法 ? ? ? ?public static void AddStudent(Dictionary<string, Student> dc) ? ? ? ? { ? ? ? ? ? ? bool Flag = true; ? ? ? ? ? ? while (Flag) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入學(xué)號(hào):"); ? ? ? ? ? ? ? ? string num = Console.ReadLine(); ? ? ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入姓名:"); ? ? ? ? ? ? ? ? string name = Console.ReadLine(); ? ? ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入年齡:"); ? ? ? ? ? ? ? ? int age = int.Parse(Console.ReadLine()); ? ? ? ? ? ? ? ? Student student = new Student(num, name, age); ? ? ? ? ? ? ? ? //判斷輸入的學(xué)號(hào)是否重復(fù) ? ? ? ? ? ? ? ? if (!dc.ContainsKey(num)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? dc.Add(student.Num, student); ? ? ? ? ? ? ? ? ? ? Console.WriteLine("添加成功"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.WriteLine("已存在,添加失敗"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? Console.WriteLine("是否繼續(xù)添加(Y/N)):"); ? ? ? ? ? ? ? ? string b = Utility.readConfirmSelection(); ? ? ? ? ? ? ? ? if (b.Equals("n")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Flag = false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? //創(chuàng)建一個(gè)輸出全部信息的方法 ? ? ? ? public static void Print(Dictionary<string,Student> dc) { ? ? ? ? ? ? //循環(huán)遍歷,將 Dictionary<K,V> 類(lèi)中的值賦值給item ? ? ? ? ? ? //需要重寫(xiě)Tostring方法,否則輸出的值為該值得命名空間 ? ? ? ? ? ? foreach (var item in dc.Values) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine(item); ? ? ? ? ? ? } ? ? ? ?? ? ? ? ? } ? ? ? ? //刪除學(xué)生信息 ? ? ? ? public static void DeleteStudent(Dictionary<string, Student> dc) { ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入要?jiǎng)h除的學(xué)生學(xué)號(hào)"); ? ? ? ? ? ? string num = Console.ReadLine(); ? ? ? ? ? ? //判斷num值是否在該類(lèi)中 ? ? ? ? ? ? if (dc.ContainsKey(num)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //根據(jù)提供的num移除目標(biāo) ? ? ? ? ? ? ? ? dc.Remove(num); ? ? ? ? ? ? ? ? Console.WriteLine("刪除成功"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("該學(xué)號(hào)的學(xué)生信息不存在"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //將排序后的元素輸出 ? ? ? ? internal static void Print(List<Student> students) ? ? ? ? { ? ? ? ? ? ? foreach (var item in students) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine(item); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //按照學(xué)號(hào)查詢(xún),如果沒(méi)查到返回查無(wú)此人 ? ? ? ? public static void QueryByNum(Dictionary<string, Student> dc) ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入你要查詢(xún)的學(xué)號(hào)"); ? ? ? ? ? ? string num = Console.ReadLine(); ? ? ? ? ? ? // ? ? ? ? ? ? if (dc.ContainsKey(num)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine(dc[num]); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("查無(wú)此人"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //按年齡排序 ? ? ? ? public static void Age(Dictionary<string, Student> dc) ? ? ? ? { ? ? ? ? ? ? List<Student> students = new List<Student>(); ? ? ? ? ? ? //字典中無(wú)序,將字典中的值存放到有序的list集合中 ? ? ? ? ? ? students.AddRange(dc.Values); ? ? ? ? ? ? //調(diào)用NameSort的方法 ? ? ? ? ? ? students.Sort(new AgeSort()); ? ? ? ? ? ? Utility.Print(students); ? ? ? ? } ? ? ? ? //按名字排序 ? ? ? ? public static void NameSort(Dictionary<string, Student> dc) ? ? ? ? { ? ? ? ? ? ? List<Student> students = new List<Student>(); ? ? ? ? ? ? //字典中無(wú)序,將字典中的值存放到有序的list集合中 ? ? ? ? ? ? students.AddRange(dc.Values); ? ? ? ? ? ? //調(diào)用NameSort的方法 ? ? ? ? ? ? students.Sort(new NameSort()); ? ? ? ? ? ? Utility.Print(students); ? ? ? ? } ? ? ? ? //按學(xué)號(hào)排序 ? ? ? ? public static void NumSort(Dictionary<string, Student> dc) ? ? ? ? { ? ? ? ? ? ? List<Student> students = new List<Student>(); ? ? ? ? ? ? //字典中無(wú)序,將字典中的值存放到有序的list集合中 ? ? ? ? ? ? students.AddRange(dc.Values); ? ? ? ? ? ? //調(diào)用NameSort的方法 ? ? ? ? ? ? students.Sort(new NumSort()); ? ? ? ? ? ? Utility.Print(students); ? ? ? ? } ? ? } }
比較器
創(chuàng)建三個(gè)比較器用于比較排序,使用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.學(xué)號(hào)比較器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _01 { ? ? //構(gòu)造比較器,進(jìn)行比較 ? ? 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<數(shù)據(jù)類(lèi)型,數(shù)據(jù)類(lèi)型> 對(duì)象名 = new Dictionary<數(shù)據(jù)類(lèi)型,數(shù)據(jù)類(lèi)型>(); ? ? ? ? ? ? Dictionary<string, Student> ha = new Dictionary<string, Student>(); ? ? ? ? ? ? //將學(xué)生類(lèi)對(duì)象添加到字典中 ? ? ? ? ? ? ha.Add(stu1.Num,stu1); ? ? ? ? ? ? ha.Add(stu2.Num,stu2); ? ? ? ? ? ? bool Flag = true; ? ? ? ? ? ? while (Flag) ? ? ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("請(qǐng)選擇:1、添加學(xué)生信息。2、刪除學(xué)生信息 3、查詢(xún)學(xué)生信息;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: ? ? ? ? ? ? ? ? ? ? ? ? //查詢(xún) ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("1、查詢(xún)所有(按學(xué)號(hào)排序)2、查詢(xún)所有(按姓名排序),3、查詢(xún)所有(按年齡排序)" + ? ? ? ? ? ? ? ? ? ? ? ?"4、按學(xué)號(hào)查詢(xún)(查沒(méi)有,則打印查無(wú)此學(xué)生)5、退出"); ? ? ? ? ? ? ? ? ? ? ? ? int q = Utility.readMenuSelection2(); ? ? ? ? ? ? ? ? ? ? ? ? switch (q) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢(xún)所有(按學(xué)號(hào)排序) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Utility.NumSort(ha); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢(xún)所有(按姓名排序) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.NameSort(ha); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢(xún)所有(按年齡排序) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.Age(ha); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //按學(xué)號(hào)查詢(xún)(查沒(méi)有,則打印查無(wú)此學(xué)生) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.QueryByNum(ha); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? case 5: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //退出 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? ? ? Console.WriteLine("確認(rèn)是否退出(Y/N)"); ? ? ? ? ? ? ? ? ? ? string b = Utility.readConfirmSelection(); ? ? ? ? ? ? ? ? ? ? if (b.Equals("y")) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? Flag = false; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //退出 ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? Console.ReadKey(); ? ? ? ? } ? ? ? } }
以上就是用一些簡(jiǎn)單的代碼完成一個(gè)簡(jiǎn)易的學(xué)生管理系統(tǒng)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
unity實(shí)現(xiàn)文字滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)文字滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別
C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過(guò)本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧2017-09-09Winform讓DataGridView左側(cè)顯示圖片
本文主要介紹在如何讓DataGridView左側(cè)顯示圖片,這里主要講解重寫(xiě)DataGridView的OnRowPostPaint方法,需要的朋友可以參考下。2016-05-05通過(guò)App.xaml理解wpf中的Application類(lèi)
這篇文章主要介紹了通過(guò)App.xaml理解wpf中的Application類(lèi),幫助大家更好的理解和學(xué)習(xí)使用c# wpf,感興趣的朋友可以了解下2021-04-04