在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();
}
}
}