struct Students {
var name: String
var studentID: Int
var gender: String
var chineseScore: Double
var mathScore: Double
var englishScore: Double
init(name: String, studentID: Int, gender: String, chineseScore: Double, mathScore: Double, englishScore: Double) {
self.name = name
self.studentID = studentID
self.gender = gender
self.chineseScore = chineseScore
self.mathScore = mathScore
self.englishScore = englishScore
}
func calculateTotalScore() -> Double {
return chineseScore + mathScore + englishScore
}
}
let s1 = Students(name: "Alice", studentID: 1, gender: "Female", chineseScore: 90, mathScore: 85, englishScore: 95)
let s2 = Students(name: "Bob", studentID: 2, gender: "Male", chineseScore: 80, mathScore: 75, englishScore: 85)
let s3 = Students(name: "Charlie", studentID: 3, gender: "Male", chineseScore: 95, mathScore: 90, englishScore: 92)
print("请输入要查找的学生姓名或学号:")
if let str = readLine() {
var foundStudent: Students?
if let studentID = Int(str) {
if studentID == s1.studentID {
foundStudent = s1
} else if studentID == s2.studentID {
foundStudent = s2
} else if studentID == s3.studentID {
foundStudent = s3
}
} else {
if str == s1.name {
foundStudent = s1
} else if str == s2.name {
foundStudent = s2
} else if str == s3.name {
foundStudent = s3
}
}
if let student = foundStudent {
print("找到学生:\(student.name)")
print("学号:\(student.studentID)")
print("性别:\(student.gender)")
print("语文成绩:\(student.chineseScore)")
print("数学成绩:\(student.mathScore)")
print("英语成绩:\(student.englishScore)")
print("总成绩:\(student.calculateTotalScore())")
} else {
print("未找到该学生。")
}
}