作業12#35 換算 App,比方小費,BMI,匯率,單位換算
Published in
3 min readJan 4, 2022
BMI換算,除了計算BMI值外,還多加了一個LABEL去看BMI值是否符合標準
import UIKitclass ViewController: UIViewController {
@IBOutlet weak var heightTextField: UITextField!
@IBOutlet weak var weightTextField: UITextField!
@IBOutlet weak var BMILabel: UILabel!@IBOutlet weak var resultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}@IBAction func calculate(_ sender: UIButton) {
let heightText = heightTextField.text!
let weightText = weightTextField.text!
let height = Double(heightText)
let weight = Double(weightText)
let heightMeter = height! / 100
if height != nil, weight != nil {
let BMI = weight! / (heightMeter * heightMeter)
BMILabel.text = String(format: "%.1f", BMI)
view.endEditing(true)
}
if weight! / (heightMeter * heightMeter) < 18.5 {
resultLabel.text = "體重過輕"
} else if weight! / (heightMeter * heightMeter) < 24 {
resultLabel.text = "標準"
} else if weight! / (heightMeter * heightMeter) < 27 {
resultLabel.text = "過重"
} else if weight! / (heightMeter * heightMeter) < 30 {
resultLabel.text = "輕度肥胖"
} else if weight! / (heightMeter * heightMeter) < 35 {
resultLabel.text = "中度肥胖"
} else {
resultLabel.text = "重度肥胖"
}
}
}