定义一个表示学生信息的类Student,要求如下: (1)类Student的成员变量: sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Jav a课程成绩。 (2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。 (3)类Student的方法成员: getNo():获得学号; getName():获得姓名; getSex():获得性别: getAge ()获得年龄; getJava():获得Java 课程成绩 根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
时间: 2023-05-22 11:03:32 浏览: 187
好的,下面是对您第一个问题的回答:
```lua
local Student = {sNO = "", sName = "", sSex = "", sAge = 0, sJava = 0}
function Student:new(sNO, sName, sSex, sAge, sJava)
local obj = {}
setmetatable(obj, self)
self.__index = self
obj.sNO = sNO
obj.sName = sName
obj.sSex = sSex
obj.sAge = sAge
obj.sJava = sJava
return obj
end
function Student:getNo()
return self.sNO
end
function Student:getName()
return self.sName
end
function Student:getSex()
return self.sSex
end
function Student:getAge()
return self.sAge
end
function Student:getJava()
return self.sJava
end
local student1 = Student:new("101", "张三", "男", 18, 90)
local student2 = Student:new("102", "李四", "男", 19, 80)
local student3 = Student:new("103", "王五", "女", 20, 85)
local student4 = Student:new("104", "赵六", "男", 21, 95)
local student5 = Student:new("105", "钱七", "女", 22, 88)
print("学生信息:")
print(student1:getNo(), student1:getName(), student1:getSex(), student1:getAge(), student1:getJava())
print(student2:getNo(), student2:getName(), student2:getSex(), student2:getAge(), student2:getJava())
print(student3:getNo(), student3:getName(), student3:getSex(), student3:getAge(), student3:getJava())
print(student4:getNo(), student4:getName(), student4:getSex(), student4:getAge(), student4:getJava())
print(student5:getNo(), student5:getName(), student5:getSex(), student5:getAge(), student5:getJava())
local sumJava = student1:getJava() + student2:getJava() + student3:getJava() + student4:getJava() + student5:getJava()
local avgJava = sumJava / 5
print("Java语言成绩的平均值:", avgJava)
local maxJava = student1:getJava()
if student2:getJava() > maxJava then
maxJava = student2:getJava()
end
if student3:getJava() > maxJava then
maxJava = student3:getJava()
end
if student4:getJava() > maxJava then
maxJava = student4:getJava()
end
if student5:getJava() > maxJava then
maxJava = student5:getJava()
end
print("Java语言成绩的最大值:", maxJava)
local minJava = student1:getJava()
if student2:getJava() < minJava then
minJava = student2:getJava()
end
if student3:getJava() < minJava then
minJava = student3:getJava()
end
if student4:getJava() < minJava then
minJava = student4:getJava()
end
if student5:getJava() < minJava then
minJava = student5:getJava()
end
print("Java语言成绩的最小值:", minJava)
```
希望这个代码能够满足您的需求。
阅读全文