作業:簡易良率計算機

參考文章:

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

附上成品:

這個想法來自於我的工作中,每張工單生產完都要進行良率結算,低於良率則需要進行補投生產,程式碼算是比較簡單好寫的,看到學長姐的作品匯率換算、BMR、單位換算、房貸借貸試算之類的,感覺很厲害,突然覺得”對世界保有強烈好奇心”,好像是工程師一個蠻重要的特質,當你懂的東西越多,你就越有想法去創造app

附上這次的程式碼及Github:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var putInTextField: UITextField!
@IBOutlet weak var yieldTextField: UITextField!
@IBOutlet weak var resultLable: UILabel!
@IBOutlet weak var goodProductTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func calculateBtn(_ sender: Any) {
let putIn = Double(putInTextField.text!)
let yield = Double(yieldTextField.text!)
let goodProduct = Double(goodProductTextField.text!)
if putIn != nil, yield != nil, goodProduct != nil{
let resultYield = goodProduct! / putIn! * 100
let need = putIn! * yield! / 100 - goodProduct!
if resultYield < yield!{
resultLable.text = "良率為\(String(format: "%.2f", resultYield))%,尚不足\(yield!)%,請補投\(String(format: "%.0f", need))以符合良率標準。"
}else{
resultLable.text = "良率為\(String(format: "%.2f", resultYield))%,以達到良率標準,請過帳至下製程。"
}
view.endEditing(true)
}

}
@IBAction func clearBtn(_ sender: Any) {
putInTextField.text = ""
yieldTextField.text = ""
goodProductTextField.text = ""
resultLable.text = ""
view.endEditing(true)
}
}

程式碼超少的啊😂

--

--