Untitled

mail@pastecode.io avatar
unknown
swift
2 years ago
1.6 kB
2
Indexable
Never
//
//  ViewController.swift
//  BMIcalculator
//
//  Created by sean on 2022/3/2.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var heightTextField: UITextField!
    
    @IBOutlet weak var weightTextField: UITextField!
    
    @IBOutlet weak var gender: UISegmentedControl!
    
    @IBOutlet weak var bmi: UILabel!
    
    @IBOutlet weak var status: UILabel!
    
    @IBAction func CalculateBMI(_ sender: Any) {
        if (heightTextField.text != nil && weightTextField.text != nil) {
            let height = Float(heightTextField.text!)!/100.0
            let weight = Float(weightTextField.text!)!
            bmi.text = String(weight/(height*height))
        }
        switch gender.selectedSegmentIndex{
        case 0:
            status.text = checkStatus(bmi: Float(bmi.text!)! )
            break
        case 1:
            status.text = checkStatus(bmi: Float(bmi.text!)!)
            if (Float(bmi.text!)! > 24.9) {
                status.text = "It's a secret"
            }
            break
            
        default:
            break
        }
    }
    
    func checkStatus(bmi: Float)->String {
        if (bmi < 18.5) {
            return "Under weight"
        }else if (bmi < 24.9) {
            return "Normal weight"
        }else if (bmi < 29.9) {
            return "Overweight"
        }else if (bmi < 39.9) {
            return "Obesity"
        }else{
            return "Extreme Obesity"
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }


}