最新国产好看的视频,伊人天堂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)對Student類的三個(gè)字段的排序。

1、學(xué)生類:學(xué)號、姓名、年齡
2、請選擇:1、添加學(xué)生信息。2、刪除學(xué)生信息 2、查詢學(xué)生信息。
3、重復(fù)的學(xué)號不能添加。
4、查詢學(xué)生信息功能中有:1、查詢所有(按學(xué)號排序)2、查詢所有(按姓名排序),2、查詢所有(按年齡排序)4、按學(xué)號查詢(查沒有,則打印查無此學(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é)號
? ? ? ? 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é)號:{Num}姓名:{Name}年齡:{Age}";
? ? ? ? }
?? ??? ?//創(chuàng)建比較器用于比較
? ? ? ? public int CompareTo(Student other)
? ? ? ? {
? ? ? ? ? ? return this.Num.CompareTo(other.Num);
? ? ? ? }

? ? }
}

工具類(Utility)

工具類中封裝了一些方法用于調(diào)用,使得主方法中的代碼簡潔

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("選擇錯誤,請重新輸入:");?
? ? ? ? ? ? } 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;
? ? ? ? }

? ? ? ? //用于確認(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("選擇錯誤,請重新輸入:");?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return b;
? ? ? ? }
? ? ? ? //創(chuàng)建添加的方法
? ? ? ?public static void AddStudent(Dictionary<string, Student> dc)
? ? ? ? {
? ? ? ? ? ? bool Flag = true;
? ? ? ? ? ? while (Flag)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("請輸入學(xué)號:");
? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? //判斷輸入的學(xué)號是否重復(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("請輸入要刪除的學(xué)生學(xué)號");
? ? ? ? ? ? string num = Console.ReadLine();
? ? ? ? ? ? //判斷num值是否在該類中
? ? ? ? ? ? if (dc.ContainsKey(num))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //根據(jù)提供的num移除目標(biāo)
? ? ? ? ? ? ? ? dc.Remove(num);
? ? ? ? ? ? ? ? Console.WriteLine("刪除成功");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("該學(xué)號的學(xué)生信息不存在");
? ? ? ? ? ? }


? ? ? ? }
? ? ? ? //將排序后的元素輸出
? ? ? ? internal static void Print(List<Student> students)
? ? ? ? {
? ? ? ? ? ? foreach (var item in students)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //按照學(xué)號查詢,如果沒查到返回查無此人
? ? ? ? public static void QueryByNum(Dictionary<string, Student> dc)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請輸入你要查詢的學(xué)號");
? ? ? ? ? ? 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é)號排序
? ? ? ? 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é)號比較器

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ù)類型> 對象名 = new Dictionary<數(shù)據(jù)類型,數(shù)據(jù)類型>();

? ? ? ? ? ? Dictionary<string, Student> ha = new Dictionary<string, Student>();
? ? ? ? ? ? //將學(xué)生類對象添加到字典中
? ? ? ? ? ? ha.Add(stu1.Num,stu1);
? ? ? ? ? ? ha.Add(stu2.Num,stu2);

? ? ? ? ? ? bool Flag = true;
? ? ? ? ? ? while (Flag)
? ? ? ? ? ? {
? ? ? ? ? ? Console.WriteLine("請選擇: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é)號排序)2、查詢所有(按姓名排序),3、查詢所有(按年齡排序)" +
? ? ? ? ? ? ? ? ? ? ? ?"4、按學(xué)號查詢(查沒有,則打印查無此學(xué)生)5、退出");
? ? ? ? ? ? ? ? ? ? ? ? int q = Utility.readMenuSelection2();
? ? ? ? ? ? ? ? ? ? ? ? switch (q)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按學(xué)號排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Utility.NumSort(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按姓名排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.NameSort(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查詢所有(按年齡排序)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Utility.Age(ha);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //按學(xué)號查詢(查沒有,則打印查無此學(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();

? ? ? ? }

?
? ? }
}

以上就是用一些簡單的代碼完成一個(gè)簡易的學(xué)生管理系統(tǒng)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity Shader實(shí)現(xiàn)序列幀動畫效果

    Unity Shader實(shí)現(xiàn)序列幀動畫效果

    這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)序列幀動畫效果 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#中 paint()與Onpaint()的區(qū)別

    C#中 paint()與Onpaint()的區(qū)別

    paint是事件onpaint方法onpaint方法是調(diào)用paint事件的,用哪一個(gè),效果是一樣,就看那一個(gè)方便了內(nèi)部是這樣實(shí)現(xiàn)的:
    2013-04-04
  • C# TabControl手動觸發(fā)DrawItem的實(shí)現(xiàn)

    C# TabControl手動觸發(fā)DrawItem的實(shí)現(xiàn)

    本文主要介紹了C# TabControl手動觸發(fā)DrawItem的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • C#使用密封類實(shí)現(xiàn)密封用戶信息的示例詳解

    C#使用密封類實(shí)現(xiàn)密封用戶信息的示例詳解

    在C#中,密封類(sealed class)是一種不能被其他類繼承的類,它用于防止其他類繼承它的功能和屬性, 下面我們就來看看如何使用密封類密封用戶的信息吧
    2024-02-02
  • C#的FileSystemWatcher用法實(shí)例詳解

    C#的FileSystemWatcher用法實(shí)例詳解

    這篇文章主要介紹了C#的FileSystemWatcher用法,以實(shí)例形似詳細(xì)分析了FileSystemWatcher控件主要功能,并總結(jié)了FileSystemWatcher控件使用的技巧,需要的朋友可以參考下
    2014-11-11
  • 解析C#彩色圖像灰度化算法的實(shí)現(xiàn)代碼詳解

    解析C#彩色圖像灰度化算法的實(shí)現(xiàn)代碼詳解

    本篇文章是對C#中彩色圖像灰度化算法的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#實(shí)現(xiàn)軟件開機(jī)自動啟動的兩種常用方法總結(jié)

    C#實(shí)現(xiàn)軟件開機(jī)自動啟動的兩種常用方法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)軟件開機(jī)自動啟動的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-07-07
  • C#關(guān)鍵字in、out、ref的作用與區(qū)別

    C#關(guān)鍵字in、out、ref的作用與區(qū)別

    這篇文章介紹了C#關(guān)鍵字in、out、ref的作用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • 如何使用C#串口通訊實(shí)現(xiàn)數(shù)據(jù)的發(fā)送和接收

    如何使用C#串口通訊實(shí)現(xiàn)數(shù)據(jù)的發(fā)送和接收

    本文詳細(xì)介紹了如何使用C#實(shí)現(xiàn)基于串口通訊的數(shù)據(jù)發(fā)送和接收,通過SerialPort類,我們可以輕松實(shí)現(xiàn)串口通訊,并結(jié)合事件機(jī)制實(shí)現(xiàn)數(shù)據(jù)的傳遞和處理,感興趣的朋友一起看看吧
    2025-03-03
  • 基于C#實(shí)現(xiàn)磁性吸附窗體

    基于C#實(shí)現(xiàn)磁性吸附窗體

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)磁性吸附窗體,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12

最新評論

施秉县| 香格里拉县| 报价| 安达市| 卫辉市| 沙雅县| 靖安县| 闸北区| 唐海县| 尉犁县| 台江县| 潍坊市| 平陆县| 民勤县| 双柏县| 浦北县| 唐河县| 浦江县| 凉山| 历史| 泾川县| 铜鼓县| 静宁县| 邳州市| 资中县| 肃北| 大理市| 东光县| 荥阳市| 武川县| 云梦县| 宝清县| 咸丰县| 红原县| 启东市| 西峡县| 柳江县| 运城市| 长宁区| 镇平县| 汝州市|