欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現學生管理系統(tǒng)

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

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

添加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; }//年齡
?? ??? ?//創(chuàng)建帶參的構造方法
? ? ? ? public Student(string num,string name,int age) {
? ? ? ? ? ? this.Num = num;
? ? ? ? ? ? this.Name = name;
? ? ? ? ? ? this.Age = age;
? ? ? ? }
?? ??? ?//創(chuàng)建無參的構造方法,在本次代碼中未使用
? ? ? ? public Student() { }
? ? ? ? //重寫了Tostring()的方法將字典中的值輸出打印
? ? ? ? public override string ToString()
? ? ? ? {

? ? ? ? ? ? return $"學號:{Num}姓名:{Name}年齡:{Age}";
? ? ? ? }
?? ??? ?//創(chuàng)建比較器用于比較
? ? ? ? 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();
? ? ? ? ? ? ? ? //將輸入的值轉換成小寫的字母,用于用戶區(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("請輸入學號:");
? ? ? ? ? ? ? ? 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("是否繼續(xù)添加(Y/N)):");
? ? ? ? ? ? ? ? string b = Utility.readConfirmSelection();
? ? ? ? ? ? ? ? if (b.Equals("n"))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Flag = false;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? //創(chuàng)建一個輸出全部信息的方法
? ? ? ? public static void Print(Dictionary<string,Student> dc) {
? ? ? ? ? ? //循環(huán)遍歷,將 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);

? ? ? ? }
? ? }
}

比較器

創(chuàng)建三個比較器用于比較排序,使用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();

? ? ? ? }

?
? ? }
}

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • unity實現文字滾動效果

    unity實現文字滾動效果

    這篇文章主要為大家詳細介紹了unity實現文字滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • C#及WPF獲取本機所有字體和顏色的方法

    C#及WPF獲取本機所有字體和顏色的方法

    這篇文章主要介紹了C#及WPF獲取本機所有字體和顏色的方法,實例分析了C#及WPF獲取本機字體及顏色的相關技巧,非常簡單實用,需要的朋友可以參考下
    2015-09-09
  • 深入分析c# 繼承

    深入分析c# 繼承

    這篇文章主要介紹了c# 繼承的相關資料,文中講解的非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07
  • C#監(jiān)控文件夾變化的方法

    C#監(jiān)控文件夾變化的方法

    這篇文章主要介紹了C#監(jiān)控文件夾變化的方法,通過FileSystemWatcher類的方法來實現對文件夾的監(jiān)控,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • 淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別

    淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別

    C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧
    2017-09-09
  • C#時間操作類分享

    C#時間操作類分享

    這篇文章主要為大家分享了C#時間操作類,秒轉換成分鐘,獲得兩個日期的間隔等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#中獲取各種文件名的實現方式

    C#中獲取各種文件名的實現方式

    這篇文章主要介紹了C#中獲取各種文件名的實現方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Winform讓DataGridView左側顯示圖片

    Winform讓DataGridView左側顯示圖片

    本文主要介紹在如何讓DataGridView左側顯示圖片,這里主要講解重寫DataGridView的OnRowPostPaint方法,需要的朋友可以參考下。
    2016-05-05
  • 通過App.xaml理解wpf中的Application類

    通過App.xaml理解wpf中的Application類

    這篇文章主要介紹了通過App.xaml理解wpf中的Application類,幫助大家更好的理解和學習使用c# wpf,感興趣的朋友可以了解下
    2021-04-04
  • c#二維碼生成的代碼分享

    c#二維碼生成的代碼分享

    c#生成二維碼實現示例代碼分享,生成方法是調用外網API,為了不直接調用別人的接口,創(chuàng)建一個QrImg.aspx用于顯示二維碼,傳參數即可
    2013-12-12

最新評論