#35.換算App: 測測BMI,為自己的健康把關吧~~~

作業目的:學習字串轉數字,optional binding,收鍵盤

閒來無事就來測一下BMI,是否符合標準值吧!
以下貼心附上我在網路上擷取的BMI範圍供大家參考唷!

上表源自網路
import UIKitclass ViewController: UIViewController {@IBOutlet weak var heightTextField: UITextField!@IBOutlet weak var weightTextField: UITextField!@IBOutlet weak var BMILabel: UILabel!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.}@IBAction func calculate(_ sender: Any) {
let heightText = heightTextField.text!let weightText = weightTextField.text!let height = Double(heightText)let weight = Double(weightText)
if height != nil, weight != nil{let heightMeter = height! / 100let bmi = weight! / ( heightMeter * heightMeter )BMILabel.text = String(format:"%.2f",bmi)}
view.endEditing(true)
}
}

--

--