let smallestgrade = (min(musicgrade, bildgrade, matematicsgrade)) (Dosent work)
等级的替代方案是:“A,B,C,D,E,F”
F是最小值,其值为0
A是最大值,值为20
如何整理出最小的值? 我可以这样做:
A = 20
B = 17.5
C = 15.0
D = 12.5
E = 10
F = 0
...
答案 0 :(得分:2)
简单,类型安全的解决方案:
enum Grade : Double {
case A = 20.0
case B = 17.5
case C = 15.0
case D = 12.5
case E = 10.0
case F = 0.0
}
extension Grade : Comparable {
static func <(lhs: Grade, rhs: Grade) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
let musicGrade = Grade.A
let bildGrade = Grade.E
let mathGrade = Grade.D
let worstGrade = [musicGrade, bildGrade, mathGrade].min()
print(worstGrade)
答案 1 :(得分:1)
也许你想要这样的事情:
let gradeValues = ["A" : 20, "B" : 17.5, "C" : 15.0, "D" : 12.5, "E" : 10, "F" : 0]
let grades = ["B", "A", "C", "A", "B"]
let minim = grades.min {gradeValues[$0]! < gradeValues[$1]!} // "C"