class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

class StudentManagementSystem:
    def __init__(self):
        self.students = []

    def add_student(self, name, age, grade):
        student = Student(name, age, grade)
        self.students.append(student)
        print("学生已添加")

    def find_student(self, name):
        for student in self.students:
            if student.name == name:
                return student
        return None

    def print_all_students(self):
        if len(self.students) == 0:
            print("当前没有学生")
        else:
            for student in self.students:
                print(f"姓名: {student.name}, 年龄: {student.age}, 成绩: {student.grade}")

# 测试学生管理系统
sms = StudentManagementSystem()

while True:
    print("-------------------------------")
    print("请选择要执行的操作:")
    print("1. 添加学生")
    print("2. 查找学生")
    print("3. 打印所有学生")
    print("4. 退出")
    choice = input("请输入选择的操作 (1/2/3/4): ")

    if choice == "1":
        name = input("请输入学生姓名: ")
        age = int(input("请输入学生年龄: "))
        grade = int(input("请输入学生成绩: "))
        sms.add_student(name, age, grade)
    elif choice == "2":
        name = input("请输入要查找的学生姓名: ")
        student = sms.find_student(name)
        if student:
            print(f"姓名: {student.name}, 年龄: {student.age}, 成绩: {student.grade}")
        else:
            print("未找到该学生")
    elif choice == "3":
        sms.print_all_students()
    elif choice == "4":
        break
    else:
        print("无效的选择")

此代码创建了两个类:Student 和 StudentManagementSystemStudent 类表示一个学生,StudentManagementSystem 类表示学生管理系统。学生管理系统具有添加学生、查找学生和打印所有学生的功能。

您可以运行此代码并按照提示进行操作,添加、查找和打印学生信息。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部