某班有 5 个学生,三门课(语文、数学、英语)。录入学生的信息并分别编写 3 个函数实现以下要求: (1) 求各门课的平均分; (2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩; (3) 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名
时间: 2023-05-19 12:03:51 浏览: 142
以下是代码实现:
```lua
-- 学生信息表
local students = {
{id = 1, name = "张三", chinese = 80, math = 90, english = 85},
{id = 2, name = "李四", chinese = 75, math = 60, english = 70},
{id = 3, name = "王五", chinese = 90, math = 85, english = 95},
{id = 4, name = "赵六", chinese = 95, math = 80, english = 90},
{id = 5, name = "钱七", chinese = 85, math = 70, english = 80},
}
-- 求各门课的平均分
function averageScore(subject)
local sum = 0
for _, student in ipairs(students) do
sum = sum + student[subject]
end
return sum / #students
end
-- 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩
function failedStudents()
local failedCount = 0
for _, student in ipairs(students) do
if student.chinese < 60 then
failedCount = failedCount + 1
print(student.id, "语文", student.chinese)
end
if student.math < 60 then
failedCount = failedCount + 1
print(student.id, "数学", student.math)
end
if student.english < 60 then
failedCount = failedCount + 1
print(student.id, "英语", student.english)
end
if failedCount >= 2 then
break
end
end
end
-- 找出三门课平均成绩在 85-90 分的学生,并输出其学号和姓名
function goodStudents()
for _, student in ipairs(students) do
local average = (student.chinese + student.math + student.english) / 3
if average >= 85 and average <= 90 then
print(student.id, student.name)
end
end
end
```
如果您有更多的问题,可以继续问我。
阅读全文