最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)

 更新時(shí)間:2022年08月03日 16:53:02   作者:提燈尋貓  
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

添加3個(gè)類,分別實(shí)現(xiàn) IComparer接口,實(shí)現(xiàn)對(duì)Student類的三個(gè)字段的排序。

1、學(xué)生類:學(xué)號(hào)、姓名、年齡
2、請(qǐng)選擇:1、添加學(xué)生信息。2、刪除學(xué)生信息 2、查詢學(xué)生信息。
3、重復(fù)的學(xué)號(hào)不能添加。
4、查詢學(xué)生信息功能中有:1、查詢所有(按學(xué)號(hào)排序)2、查詢所有(按姓名排序),2、查詢所有(按年齡排序)4、按學(xué)號(hào)查詢(查沒有,則打印查無此學(xué)生)5、退出

學(xué)生類

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)建無參的構(gòu)造方法,在本次代碼中未使用
? ? ? ? public Student() { }
? ? ? ? //重寫了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);
? ? ? ? }

? ? }
}

工具類(Utility)

工具類中封裝了一些方法用于調(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)選擇的輸入。該方法從鍵盤讀取‘Y'或'N',并將其作為方法的返回值。
? ? ? ? public static string readConfirmSelection()
? ? ? ? {
? ? ? ? ? ? string b;
? ? ? ? ? ? for (; ; )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? b = Console.ReadLine();
? ? ? ? ? ? ? ? //將輸入的值轉(zhuǎn)換成小寫的字母,用于用戶區(qū)分大小寫的輸入
? ? ? ? ? ? ? ? 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> 類中的值賦值給item
? ? ? ? ? ? //需要重寫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值是否在該類中
? ? ? ? ? ? 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)查詢,如果沒查到返回查無此人
? ? ? ? public static void QueryByNum(Dictionary<string, Student> dc)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請(qǐng)輸入你要查詢的學(xué)號(hào)");
? ? ? ? ? ? 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);
? ? ? ? ? ? //調(diào)用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);
? ? ? ? ? ? //調(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>();
? ? ? ? ? ? //字典中無序,將字典中的值存放到有序的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ù)類型,數(shù)據(jù)類型> 對(duì)象名 = new Dictionary<數(shù)據(jù)類型,數(shù)據(jù)類型>();

? ? ? ? ? ? Dictionary<string, Student> ha = new Dictionary<string, Student>();
? ? ? ? ? ? //將學(xué)生類對(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、查詢學(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:
? ? ? ? ? ? ? ? ? ? ? ? //查詢
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("1、查詢所有(按學(xué)號(hào)排序)2、查詢所有(按姓名排序),3、查詢所有(按年齡排序)" +
? ? ? ? ? ? ? ? ? ? ? ?"4、按學(xué)號(hào)查詢(查沒有,則打印查無此學(xué)生)5、退出");
? ? ? ? ? ? ? ? ? ? ? ? int q = Utility.readMenuSelection2();
? ? ? ? ? ? ? ? ? ? ? ? switch (q)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按學(xué)號(hào)排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Utility.NumSort(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按姓名排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.NameSort(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按年齡排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.Age(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //按學(xué)號(hào)查詢(查沒有,則打印查無此學(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)文章

最新評(píng)論

义乌市| 松溪县| 武夷山市| 得荣县| 丘北县| 玉林市| 乐陵市| 龙井市| 隆回县| 河曲县| 苍南县| 滕州市| 杨浦区| 望江县| 安阳市| 夏邑县| 宜兰县| 通州区| 昌吉市| 永济市| 曲沃县| 巴林左旗| 鄂尔多斯市| 凉山| 洞头县| 长岭县| 博湖县| 开江县| 重庆市| 宁蒗| 商南县| 长顺县| 林芝县| 岫岩| 绥中县| 隆化县| 崇州市| 乐至县| 永嘉县| 玛曲县| 南雄市|