C#利用控制台完成学生信息系统

在C#中创建一个简单的学生管理系统,通常涉及到对学生信息的处理,如添加、显示、删除和修改学生信息。这里,我将指导你如何使用控制台应用程序来实现一个基本的学生管理系统。

步骤 1: 定义学生类

首先,我们需要一个Student类来存储学生的基本信息,如姓名、年龄和学号。

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

namespace 控制台学生信息系统
{
    internal class Student
    {
        private string id;
        public string Id
        {
          get { return id; }
           set { id = value; }
        }            
        private string name;
        public string Name 
        {
            get { return name; }
            set { name = value; }
        }
        private int age;
        public int Age 
        {
            get { return age; }
            set { age = value; }
        }
        private string sex;
        public string Sex 
        {
            get { return sex; }
            set { sex = value; }
        }
        private int math;
        public int Math 
        {
            get { return math; }
            set { math = value; }
        }
        private int chinese;
        public int Chinese 
        {
            get { return chinese; }
            set { chinese = value; }
        }
        private int english;
        public int English 
        {
          get{ return english; }
          set { english = value; }    
        }
    }
}

步骤 2: 创建管理员类

接下来,创建一个User类来管理学生信息,包括管理员的id、密码、电话号码

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

namespace 控制台学生信息系统
{
    internal class User
    {
        
       private string passname = "admin";
        public string Passname
        {
            get { return passname; }
            set { passname = value; }
        }
        private string password="12345";
        public string Password 
        {
            get { return password; }
            set { password = value; }
        }
        private string number = "18700638072";
        public string Number 
        {
            get { return number; }
            set { number = value; }
        }

    }
}

 

步骤 3: 创建登陆类

接下来,创建一个Login类来判断用户登陆、修改密码等操作

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

namespace 控制台学生信息系统
{
    internal class Login
    {

        public void Show()
        {
            Console.Clear();
            Console.WriteLine("*******************************");
            Console.WriteLine("        学生信息管理页面              ");
            Console.WriteLine("********************************");
            User user = new User();
            Console.WriteLine("1.忘记密码");
            Console.WriteLine("2.用户登陆");
            int num = Convert.ToInt32(Console.ReadLine());
            switch (num)
            {
                case 1: Forget(); break;
                case 2: UserLogin(user.Passname, user.Password); break;
            }
        }
        public void Forget()
        {
            User user = new User();
            Console.WriteLine("请输入你的用户名:");
            string username = Console.ReadLine();
            Console.WriteLine("请输入你的电话号码:");
            string number = Console.ReadLine();
            string userpassword = "";
            string userpassword2 = "";
   A:         if (number == user.Number && user.Passname == username)
            {
                Console.WriteLine("请输入密码:");
              
                while (true)
                {
                    ConsoleKeyInfo ck = Console.ReadKey(true);

                    if (ck.Key != ConsoleKey.Enter)
                    {
                        if (ck.Key != ConsoleKey.Backspace)
                        {
                            userpassword += ck.KeyChar.ToString();
                            Console.Write("*");
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        break;
                    }

                }
                Console.WriteLine("请再次输入确认密码:");
               
                while (true)
                {
                    ConsoleKeyInfo ck = Console.ReadKey(true);

                    if (ck.Key != ConsoleKey.Enter)
                    {
                        if (ck.Key != ConsoleKey.Backspace)
                        {
                            userpassword2 += ck.KeyChar.ToString();
                            Console.Write("*");
                        }
                    }
                    else
                    {
                        Console.WriteLine();
                        break;
                    }

                }
            }
            if (userpassword == userpassword2)
            {
                user.Password = userpassword;
                 Console.Clear();
            }
            else { goto A; }
            UserLogin(user.Passname, user.Password);

        }
        public void  UserLogin(string a, string b)
        {
            
            Console.WriteLine("请输入你的账号密码:");
            Console.Write("账号:");
            string username = Console.ReadLine();
            Console.Write("密码:");
            string userword = "";
            while (true)
            {
                ConsoleKeyInfo ck = Console.ReadKey(true);

                if (ck.Key != ConsoleKey.Enter)
                {
                     if (ck.Key != ConsoleKey.Backspace) { 
                        userword += ck.KeyChar.ToString();
                    Console.Write("*");
                }
                }
                    else
                    {
                     Console.WriteLine();
                    break;
                    }
                }
            
            string str = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHGKLMNPQRSTUVWXYZ";
            Random rnd = new Random();
            string str1 = null;
            for (int i = 0; i < 4; i++)
            {
                str1 = str1 + str[rnd.Next(55)];
            }
            Console.WriteLine("验证码:" + str1);
            Console.Write("输入验证码:");
            string userstr = Console.ReadLine();
            if (username !=a || userword != b)
            {
                Console.WriteLine("账号密码输入错误!");
                UserLogin( a,  b);
            }
            
            if (userstr != str1) 
            { 
                Console.WriteLine("验证码输入错误!");
                UserLogin( a,  b);
            }
            if (username == a && userword == b && userstr == str1)
            {
                Console.WriteLine("登陆成功!");
                Funcation funcation=new Funcation();
                Thread.Sleep(1000);
                funcation.xuanze();
            }
        }
    }
}

步骤 4: 创建功能类

接下来,创建一个Funcation类来支持用户对学生进行增删改查操作

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

using 控制台学生信息系统;

namespace 控制台学生信息系统
{
    internal class Funcation:Login
        
     {
        List<Student> student = new List<Student>();
        public   Funcation()
        {
            student.Add(new Student { Id = "1001", Name = "张圆", Age = 15, Sex = "女", Math = 88, Chinese = 80, English = 78 });
            student.Add(new Student { Id = "1002", Name = "世圆", Age = 17, Sex = "男", Math = 98, Chinese = 99, English = 78 });
            student.Add(new Student { Id = "1003", Name = "张世", Age = 17, Sex = "男", Math = 78, Chinese = 88, English = 78 });  
        }

        //public Funcation(List<Student> student)
        //{
        //    this.student = student;
        //}

        public  void xuanze()
            {
               Console.Clear();
               //Login login = new Login();
               Console.WriteLine("1.查询学生信息");
                Console.WriteLine("2.录入学生信息");
                Console.WriteLine("3.修改学生信息");
                Console.WriteLine("4.删除学生信息");
                Console.WriteLine("5.退出操作界面");
                int num = Convert.ToInt32(Console.ReadLine());
                switch (num)
                {
                    case 1: chaxun(); break;
                    case 2: luru(); break;
                    case 3: xiugai(); break;
                    case 4: shanchu(); break;
                    case 5:Show(); break;
            }
            }
          public   void chaxun()
            {
              Console.Clear();
               Console.WriteLine("输入你要查询的姓名或者学号");
                string str2 = Console.ReadLine();
                for (int i = 0; i < student.Count; i++)
                {
                    if (str2 == student[i].Name || str2 == student[i].Id)
                    {
                        Console.WriteLine("学号:" + student[i].Id + "\t" + "姓名:" + student[i].Name + "\t" + "性别:" + student[i].Sex);
                        Console.WriteLine("数学:" + student[i].Math + "\t" + "语文:" + student[i].Chinese + "\t" + "英语:" + student[i].English);
                        ConsoleKeyInfo ck = Console.ReadKey(true);
                        if (ck.Key == ConsoleKey.Enter)
                        {
                            xuanze();
                            break;
                        }

                    }

                }
                Console.WriteLine("输入的姓名或者学号不存在");
                Thread.Sleep(1000);

                chaxun();
            }
        public void luru()
            {
            
               Console.Clear();
                Console.WriteLine("请输入学生的信息:");
                Console.WriteLine("学号:");
                string ids = Console.ReadLine();
                Console.WriteLine("姓名:");
                string names = Console.ReadLine();
                Console.WriteLine("年龄:");
                int ages = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("性别:");
                string sexs = Console.ReadLine();
                Console.WriteLine("数学:");
                int maths = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("语文:");
                int chineses = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("英语:");
                int englishs = Convert.ToInt32(Console.ReadLine());
                student.Add(new Student { Id = ids, Name = names, Age = 15, Sex = sexs, Math = maths, Chinese = chineses, English = englishs });
                Console.WriteLine("录入成功!");
                ConsoleKeyInfo ck = Console.ReadKey(true);
                if (ck.Key == ConsoleKey.Enter)
                {

                    xuanze();
                }
            }
        public void xiugai()
            {
            Console.Clear();
            Console.WriteLine("输入你要修改学生信息的姓名或者学号");
                string str2 = Console.ReadLine();
                for (int i = 0; i < student.Count; i++)
                {
                    if (str2 == student[i].Name || str2 == student[i].Id)
                    {
                        Console.WriteLine("输入你要修改的信息:");
                        string str = Console.ReadLine();
                        if (str == "学号")
                        {
                            Console.Write(student[i].Id + "修改为:");
                            string strid = Console.ReadLine();
                            student[i].Id = strid;
                        }
                        if (str == "姓名")
                        {
                            Console.Write(student[i].Name + "修改为:");
                            string strname = Console.ReadLine();
                            student[i].Name = strname;
                        }
                        if (str == "性别")
                        {
                            Console.Write(student[i].Sex + "修改为:");
                            string strsex = Console.ReadLine();
                            student[i].Sex = strsex;
                        }
                        if (str == "年龄")
                        {
                            Console.Write(student[i].Age + "修改为:");
                            int strage = Convert.ToInt32(Console.ReadLine());
                            student[i].Age = strage;
                        }
                        if (str == "语文")
                        {
                            Console.Write(student[i].Chinese + "修改为:");
                            int strchinese = Convert.ToInt32(Console.ReadLine());
                            student[i].Chinese = strchinese;
                        }
                        if (str == "英语")
                        {
                            Console.Write(student[i].English + "修改为:");
                            int strenglish = Convert.ToInt32(Console.ReadLine());
                            student[i].English = strenglish;
                        }
                        if (str == "数学")
                        {
                            Console.Write(student[i].Math + "修改为:");
                            int strmath = Convert.ToInt32(Console.ReadLine());
                            student[i].Math = strmath;
                        }
                        ConsoleKeyInfo ck = Console.ReadKey(true);
                        if (ck.Key == ConsoleKey.Enter)
                        {

                            xuanze();
                        }
                        break;
                    }

                }
            }
        public void shanchu()
            {
            Console.Clear();
            Console.WriteLine("输入你要删除学生的姓名或者学号");
                string strdelete = Console.ReadLine();
                for (int i = 0; i < student.Count; i++)
                {
                    if (student[i].Name == strdelete || student[i].Id == strdelete)
                    {
                        student.Remove(student[i]);
                        Console.WriteLine("删除成功!");
                        ConsoleKeyInfo ck = Console.ReadKey(true);
                        if (ck.Key == ConsoleKey.Enter)
                        {

                            xuanze();
                        }
                    }
                }
                Console.WriteLine("没有这个学生或者学号");
                shanchu();
            }
        }
    }

步骤 5: 启动项目

接下来,创建一个Main()类来支持用户对学生进行增删改查操作

using System.Security.Cryptography.X509Certificates;

namespace 控制台学生信息系统
{
    internal class Program
    {
      public  static void Main(string[] args)
        {
            Login login = new Login();
            login.Show();
        }
}
}

 

  • 17
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我先给你介绍一下学生选课系统的功能设计。一般来说,学生选课系统需要包含以下功能: 1. 学生登录和注册:学生可以通过账号密码登录选课系统,也可以进行注册。 2. 选课和退课:学生可以在选课期间内选择自己想要的课程,也可以在一定时间内退选。 3. 课程管理:管理员可以添加、修改、删除课程信息,包括课程名称、课程编号、授课教师、上课时间等。 4. 学生管理:管理员可以添加、修改、删除学生信息,包括学生姓名、学号、所属班级等。 5. 成绩管理:管理员可以录入学生的成绩,并对学生成绩进行查询、统计和导出。 在实现这些功能的同时,我们需要使用到数据库。数据库的设计需要包括学生表、课程表和成绩表。学生表和课程表分别存储学生和课程的基本信息,成绩表则记录每个学生在每门课程中的成绩。 在控制台应用程序中实现学生选课系统,需要使用到 C# 的相关技术,比如 ADO.NET、LINQ to SQL 等。可以使用 ADO.NET 连接数据库,进行数据的增删改查操作。同时,也可以使用 LINQ to SQL 技术快速地实现数据库访问和操作。 除此之外,还需要设计合理的界面,使得用户能够方便地进行操作。可以使用 Console.ReadLine() 和 Console.Write() 等方法实现用户输入和输出。同时,也可以使用 Console.ForegroundColor 属性设置控制台文字的颜色,使得界面更加美观。 以上是关于 C# 基于数据库和控制台学生选课系统的简单介绍,希望能够帮助到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值