帮我写一个C#学生管理系统
时间: 2023-08-02 17:41:56 浏览: 56
好的,以下是一个简单的C#学生管理系统的实现,包括添加、删除、修改和查询学生信息的功能:
```csharp
using System;
using System.Collections.Generic;
namespace StudentManagementSystem
{
class Program
{
static List<Student> students = new List<Student>(); // 初始化学生列表
static void Main(string[] args)
{
while (true)
{
Console.Clear(); // 清空控制台
Console.WriteLine("请选择操作:");
Console.WriteLine("1. 添加学生");
Console.WriteLine("2. 删除学生");
Console.WriteLine("3. 修改学生");
Console.WriteLine("4. 查询学生");
Console.WriteLine("5. 退出程序");
switch (Console.ReadLine())
{
case "1":
AddStudent();
break;
case "2":
RemoveStudent();
break;
case "3":
UpdateStudent();
break;
case "4":
SearchStudent();
break;
case "5":
Environment.Exit(0); // 退出程序
break;
default:
Console.WriteLine("无效操作,请重新选择!");
break;
}
Console.WriteLine("按任意键继续...");
Console.ReadKey();
}
}
static void AddStudent()
{
Console.WriteLine("请输入学生姓名:");
string name = Console.ReadLine();
Console.WriteLine("请输入学生年龄:");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("请输入学生性别(男/女):");
string gender = Console.ReadLine();
Console.WriteLine("请输入学生学号:");
string id = Console.ReadLine();
Console.WriteLine("请输入学生专业:");
string major = Console.ReadLine();
Student student = new Student(name, age, gender, id, major);
students.Add(student);
Console.WriteLine("添加成功!");
}
static void RemoveStudent()
{
Console.WriteLine("请输入要删除的学生学号:");
string id = Console.ReadLine();
bool found = false;
for (int i = 0; i < students.Count; i++)
{
if (students[i].Id == id)
{
students.RemoveAt(i);
Console.WriteLine("删除成功!");
found = true;
break;
}
}
if (!found)
{
Console.WriteLine("未找到该学生!");
}
}
static void UpdateStudent()
{
Console.WriteLine("请输入要修改的学生学号:");
string id = Console.ReadLine();
bool found = false;
for (int i = 0; i < students.Count; i++)
{
if (students[i].Id == id)
{
Console.WriteLine("请输入修改后的学生姓名(留空表示不修改):");
string name = Console.ReadLine();
if (!string.IsNullOrEmpty(name))
{
students[i].Name = name;
}
Console.WriteLine("请输入修改后的学生年龄(留空表示不修改):");
string ageStr = Console.ReadLine();
if (!string.IsNullOrEmpty(ageStr))
{
students[i].Age = int.Parse(ageStr);
}
Console.WriteLine("请输入修改后的学生性别(留空表示不修改):");
string gender = Console.ReadLine();
if (!string.IsNullOrEmpty(gender))
{
students[i].Gender = gender;
}
Console.WriteLine("请输入修改后的学生专业(留空表示不修改):");
string major = Console.ReadLine();
if (!string.IsNullOrEmpty(major))
{
students[i].Major = major;
}
Console.WriteLine("修改成功!");
found = true;
break;
}
}
if (!found)
{
Console.WriteLine("未找到该学生!");
}
}
static void SearchStudent()
{
Console.WriteLine("请输入要查询的学生学号:");
string id = Console.ReadLine();
bool found = false;
for (int i = 0; i < students.Count; i++)
{
if (students[i].Id == id)
{
Console.WriteLine($"姓名:{students[i].Name}");
Console.WriteLine($"年龄:{students[i].Age}");
Console.WriteLine($"性别:{students[i].Gender}");
Console.WriteLine($"学号:{students[i].Id}");
Console.WriteLine($"专业:{students[i].Major}");
found = true;
break;
}
}
if (!found)
{
Console.WriteLine("未找到该学生!");
}
}
}
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Id { get; set; }
public string Major { get; set; }
public Student(string name, int age, string gender, string id, string major)
{
Name = name;
Age = age;
Gender = gender;
Id = id;
Major = major;
}
}
}
```
这个学生管理系统使用了一个名为`Student`的类来存储学生信息,主程序中使用了一个`List<Student>`来存储所有学生的信息。通过不同的选项,可以实现对学生信息的增删改查操作。在实现自己的课设时,可以根据需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)