我試圖構建一個if/then基本應用程序,該按鈕被按下後,將在標籤中的.swift文件中顯示輸入值的聯邦所得稅。我打算以後讓代碼更加有用,但是想測試我的代碼。唯一的問題是,當我按下按鈕時,它顯示0.0,當它應該在40左右......有人知道我在做什麼錯了嗎?Else If Statement Swift
@IBOutlet weak var Label: UILabel!
@IBAction func ButtonClicked(_ sender: Any) {
var hourly:Float = 8.00
var hours:Float = 61.75
var exemptions:Float = 1.00
var deductions:Float = 95.80
var allowances:Float = (exemptions * deductions)
var gross:Float = (hourly * hours)
var taxable:Float = (gross - allowances)
if taxable >= 17529 {
var fedIncome = ((taxable * 0.396) + 5062.69)}
else if taxable < 17529 && taxable >= 17458 {
var fedIncome = ((taxable * 0.35) + 5037.84)}
else if taxable < 17458 && taxable >= 8081 {
var fedIncome = ((taxable * 0.33) + 1943.43)}
else if taxable < 8081 && taxable >= 3925 {
var fedIncome = ((taxable * 0.28) + 779.75)}
else if taxable < 3925 && taxable >= 1677 {
var fedIncome = ((taxable * 0.25) + 217.75)}
else if taxable < 1677 && taxable >= 484 {
var fedIncome = ((taxable * 0.15) + 38.80)}
else if taxable < 484 && taxable >= 96 {
var fedIncome = (taxable * 0.1)}
else {
var fedIncome:Float = 0.0}
Label.text = ("\(fedIncome)")
}
你代碼wou ld使用帶範圍的'switch'語句而不是'if' /'else if' /'else' block – Alexander