C#实现简单的学生管理系统增删改查

1.Programs.cs

定义了增删改查的方法,使用Dictionary<T,U>集合

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎使用2018级学生管理系统");
            Console.WriteLine("===============================================");
            Console.WriteLine("请选择如下操作:");
            Console.WriteLine("A:添加  B:查询  C:删除  D:修改  E退出系统");
            string ch;
         //   Hashtable stu = new Hashtable();
            Dictionary<string, Student> stu = new Dictionary<string, Student>();
            Student student=new Student();
            Console.Write("请输入选择功能:");
            ch = Console.ReadLine();
            while(ch!="E")
            {
                switch (ch)
                {
                    case "A":
                        AddStudent(stu);
                        break;
                       
                    case "B":
                        QueryStudent(stu);
                        break;

                    case "C":
                        DeleteStudent(stu);
                        break;
                    case "D":
                        UpdateStudent(stu);
                        break;
                    case "E":
                        break;
                }

                Console.Write("请输入选择功能:");
                ch = Console.ReadLine();
            }
            foreach(KeyValuePair<string,Student> de in stu)
            {
                Console.WriteLine(de.Value);
            }
           // Console.WriteLine(student.showMSG());

            Console.ReadLine();
            
        }
        public static void AddStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Console.WriteLine("请输入添加学生信息:");
            Console.WriteLine("请输入学生学号:");
            stuID = Console.ReadLine();
            while (dic.ContainsKey(stuID))
            {
                Console.WriteLine("该学生已存在,请重新输入:");
                stuID = Console.ReadLine();
            }
            Student student = new Student();

           
            Console.WriteLine("请输入学生姓名:");
            string Name = Console.ReadLine();
            Console.WriteLine("请输入学生性别:");
            string Sex = Console.ReadLine();
            Console.WriteLine("请输入学生班级:");
            string Class = Console.ReadLine();
            Console.WriteLine("请输入学生语文成绩:");
            double Chinese = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入学生数学成绩:");
            double Math = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入学生英语成绩:");
            double English = double.Parse(Console.ReadLine());
            student.setStuID(stuID);
            student.setName(Name);
            student.setSex(Sex);
            student.setClass(Class);
            student.setChinese(Chinese);
            student.setMath(Math);
            student.setEnglish(English);
            dic.Add(stuID, student);
            Console.WriteLine("添加学生成功");
        }
        public static void QueryStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Student student = new Student();
            Console.WriteLine("请输入查询学生的学号:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("该学生不存在,请重新输入:");
                stuID=Console.ReadLine();
            }
            dic[stuID].showMSG();
        }
        public static void DeleteStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Console.WriteLine("请输入删除学生的学号:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("该学生不存在,请重新输入:");
                stuID=Console.ReadLine();
            }
            dic.Remove(stuID);
            Console.WriteLine("成功删除。");
        }
        public static void UpdateStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            string id;
            Console.WriteLine("请输入修改学生的学号:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("该学生不存在,请重新输入:");
                stuID = Console.ReadLine();
            }

            Console.WriteLine("1.修改姓名  2.修改性别  3.修改班级  4.修改语文成绩  5.修改数学成绩  6.修改英语成绩");
            Console.WriteLine("请输入选择修改的序号:");
            id = Console.ReadLine();
            switch(id)
            {
                case "1":
                   Console.WriteLine("请输入学生姓名:");
                   string Name = Console.ReadLine();
                    dic[stuID].setName(Name);   
                    break;
                case "2":
                     Console.WriteLine("请输入学生性别:");
                     string Sex = Console.ReadLine();
                     dic[stuID].setSex(Sex);
                    break;
                case "3":
                     Console.WriteLine("请输入学生班级:");
                     string Class = Console.ReadLine();
                     dic[stuID].setClass(Class);
                     break;
                case "4":
                     Console.WriteLine("请输入学生语文成绩:");
                     double Chinese = double.Parse(Console.ReadLine());
                     dic[stuID].setChinese(Chinese);
                     break;
                case "5":
                     Console.WriteLine("请输入学生数学成绩:");
                     double Math = double.Parse(Console.ReadLine());
                     dic[stuID].setMath(Math);
                     break;
                case "6":
                    Console.WriteLine("请输入学生英语成绩:");
                    double English = double.Parse(Console.ReadLine());
                     dic[stuID].setEnglish(English);
                     break;

            }


        }

    }
}

2.Student.cs(学生类)

学生类中定义了几种常见属性,以及一个计算平均值的方法和一个输出的方法。在Program.cs中实例化该类的对象即可调用该方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentSystem
{
    class Student
    {
        string stuID;
        string Name;
        string Sex;
        string Class;
        double Chinese;
        double Math;
        double English;

        public Student(string stuID, string Name, string Sex, string Class, double Chinese, double Math, double English)
        {
            this.stuID = stuID;
        }
        public Student()
        { }

        public void setStuID(string stuID)
        {
            this.stuID = stuID;
        }
        public void setName(string Name)
        {
            this.Name = Name;
        }
        public void setSex(string Sex)
        {
            this.Sex = Sex;
        }
        public void setClass(string Class)
        {
            this.Class = Class;
        }
        public void setChinese(double Chinese)
        {
            this.Chinese = Chinese;
        }
        public void setMath(double Math)
        {
            this.Math = Math;
        }
        public void setEnglish(double English)
        {
            this.English = English;
        }
        public double ComputeAvg(double Chinese, double Math, double English)
        {
            this.Chinese = Chinese;
            this.Math = Math;
            this.English = English;
            double sum, avg;
            sum = Chinese + Math + English;
            avg = sum / 3;
            return avg;
        }
        public void showMSG()
        {
            Console.WriteLine(" 学号:" + stuID + " 姓名:" + Name + " 班级 "+Class+" 性别 " + Sex + " 语文" + Chinese + " 数学" + Math + "英语 " + English+" 平均成绩 "+ ComputeAvg(Chinese,Math,English));

        }
        
       

    }
}

 

展开阅读全文

没有更多推荐了,返回首页