我是Swift的新手,我正在尝试为Mac OS X创建一个登录程序。我确实知道iOS上的Swift编程,但我对OS X Swift知之甚少。所以我为OS X编写了一个程序。我几乎完成了它,但是我想用整数检查我的布尔细节,例如我想让学生的得分等于我的变量,它显示你得到的等级
class ViewController: NSViewController {
@IBOutlet var Maths: NSTextField! = NSTextField()
@IBOutlet var Science: NSTextField! = NSTextField()
@IBOutlet var English: NSTextField! = NSTextField()
@IBOutlet var Religious: NSTextField! = NSTextField()
@IBOutlet var History: NSTextField! = NSTextField()
@IBOutlet var Geography: NSTextField! = NSTextField()
@IBOutlet var ICT: NSTextField! = NSTextField()
@IBOutlet var Computing: NSTextField! = NSTextField()
@IBOutlet var Total: NSTextField!
@IBOutlet var AVG: NSTextField!
@IBOutlet var StudentName: NSTextField!
@IBOutlet var StudentClass: NSTextField!
@IBOutlet var Report: NSTextField!
var A = 80 > 100
var B = 75 > 80
var C = 60 > 75
var D = 50 > 60
var E = 40 > 50
var F = 0 > 40
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func CountTotalnAVG(sender: NSButton) {
var Mathsc = Maths.stringValue.toInt()
var Sciencec = Science.stringValue.toInt()
var Englishc = English.stringValue.toInt()
var Religiousc = Religious.stringValue.toInt()
var Historyc = History.stringValue.toInt()
var Geographyc = Geography.stringValue.toInt()
var ICTc = ICT.stringValue.toInt()
var computingc = Computing.stringValue.toInt()
/*
var add = (Mathsc?.advancedBy(Sciencec!).advancedBy(Englishc!).advancedBy(Religiousc!).advancedBy(Historyc!).advancedBy(Geographyc!).advancedBy(ICTc!).advancedBy(computingc!))
*/
var add: Int = (Mathsc! + Sciencec! + Englishc! + Religiousc! + Historyc! + Geographyc! + ICTc! + computingc!)
Total.stringValue = "\(add)"
var divide:Int = add/8
AVG.stringValue = "\(divide)"
}
@IBAction func GetReport(sender: NSButton) {
var A = 80 > 100
var B = 75 > 80
var C = 60 > 75
var D = 50 > 60
var E = 40 > 50
var F = 0 > 40
var Mathsc = Maths.stringValue.toInt()
var Sciencec = Science.stringValue.toInt()
var Englishc = English.stringValue.toInt()
var Religiousc = Religious.stringValue.toInt()
var Historyc = History.stringValue.toInt()
var Geographyc = Geography.stringValue.toInt()
var ICTc = ICT.stringValue.toInt()
var computingc = Computing.stringValue.toInt()
if Mathsc = A.boolValue {
}
}
}
答案 0 :(得分:3)
您可以使用switch
:
func getGrade(score: Int) -> String {
let result: String
switch score {
case 80...100 :
result = "A"
case 75..<80 :
result = "B"
case 60..<75 :
result = "C"
case 50..<60 :
result = "D"
case 40..<50 :
result = "E"
case 0..<40 :
result = "F"
default:
result = "F"
}
return result
}
let mathsStr = "42"
let mathsNum = mathsStr.toInt()
println(getGrade(mathsNum!)) // prints "E"