完成以下要求: D 利用Map,以班级号作为键(String类型),全班的学生作为值,描述以上所有班级信息.将Map中班级对应的班级学生信息输出 打印输出每一个班级的平均分数 4统计所有班级的女生的人数,并打印输出
时间: 2023-05-21 18:05:38 浏览: 89
非常感谢您的问题!以下是C知道的回答:
```lua
-- 班级和学生信息
local class1 = {
"Tom",
"Mike",
"Alice",
"Lily",
"Emma",
"Bob"
}
local class2 = {
"Jack",
"Mary",
"Lucy",
"Grace",
"Amy",
"John"
}
local class3 = {
"Frank",
"David",
"Steven",
"Emily",
"Jessie",
"Kelly"
}
-- 将班级号作为键,班级信息作为值保存在Map中
local classMap = {
["001"] = class1,
["002"] = class2,
["003"] = class3
}
-- 输出每个班级的学生信息
for key, value in pairs(classMap) do
print("班级号:" .. key .. ",班级学生信息:" .. table.concat(value, ", "))
end
-- 计算每个班级的平均分数
-- 这里可以根据具体情况自行定义每个学生的分数信息
local scores = {
["Tom"] = 90,
["Mike"] = 85,
["Alice"] = 95,
["Lily"] = 92,
["Emma"] = 88,
["Bob"] = 87,
["Jack"] = 91,
["Mary"] = 84,
["Lucy"] = 89,
["Grace"] = 93,
["Amy"] = 90,
["John"] = 88,
["Frank"] = 94,
["David"] = 91,
["Steven"] = 89,
["Emily"] = 92,
["Jessie"] = 86,
["Kelly"] = 85
}
for key, value in pairs(classMap) do
local sum = 0
for i = 1, #value do
sum = sum + scores[value[i]]
end
local average = sum / #value
print("班级号:" .. key .. ",平均分:" .. average)
end
-- 计算所有班级女生的人数
local femaleCount = 0
for key, value in pairs(classMap) do
for i = 1, #value do
if string.sub(value[i], -1) == "a" then
femaleCount = femaleCount + 1
end
end
end
print("所有班级女生的人数:" .. femaleCount)
```
阅读全文