酷代码 AI
菜单
服务商

创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。 浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365 注意:未用学生类对象封装数据的,本题计0分 输入格式: 依次输入3个学生的每门课成绩,每个学生成绩信息格式: 学号+英文空格+姓名+英文空格+课程名+英文空格+平时成绩+英文空格+期末成绩 注:3个学生的课程顺序可能会不一致 例如: 22201311 张琳 语文 70 80 22201311 张琳 数学 85 89 22201311

```python class Score: def __init__(self, usual_score, final_score): # 初始化平时成绩和期末成绩 self.usual_score = int(usual_score) self.final_score = int(final_score) def calculate_total_score(self): # 计算总成绩,平时成绩占40%,期末成绩占60%,取整数部分 return int(self.usual_score * 0.4 + self.final_score * 0.6) class Student: def __init__(self, student_id, name): # 初始化学号、姓名 self.student_id = student_id self.name = name # 初始化语文、数学、物理成绩为None self.chinese_score = None self.math_score = None self.physics_score = None def calculate_total_score(self): # 计算学生的总分 return self.chinese_score.calculate_total_score() + self.math_score.calculate_total_score() + self.physics_score.calculate_total_score() def calculate_average_score(self): # 计算学生的平均分,保留两位小数 return round(self.calculate_total_score() / 3, 2) students = {} for _ in range(9): info = input().split() student_id, name, course, usual_score, final_score = info if student_id not in students: # 如果学生不存在,创建新的学生对象 students[student_id] = Student(student_id, name) student = students[student_id] score = Score(usual_score, final_score) if course == "语文": student.chinese_score = score elif course == "数学": student.math_score = score elif course == "物理": student.physics_score = score for student in students.values(): total_score = student.calculate_total_score() average_score = student.calculate_average_score() chinese_total = student.chinese_score.calculate_total_score() math_total = student.math_score.calculate_total_score() physics_total = student.physics_score.calculate_total_score() print(f"{student.student_id} {student.name} {total_score} {average_score:.2f} {chinese_total:.2f} {math_total:.2f} {physics_total:.2f}") ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 输入数据的格式必须严格按照要求,若输入数据格式错误,可能导致程序无法正确解析数据。 2. 在计算总成绩和平均分时,要注意数据类型的转换,避免出现计算错误。 3. 确保每个学生的三门课程成绩都被正确赋值,否则在计算总分和平均分的时候会出现异常。 [2025-10-30 16:52:22 | AI写代码神器 | 629点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多