DEV Community

Too Leng
Too Leng

Posted on • Edited on

【ChaoCode】 Swift 基礎篇 9:流程控制「if else」 作業

在一串 if 的語法中,else if 和 else 分別最多和最少可以使用幾個?

else if:0 到無限多個。
else:0 或 1 個。
請問下面左右的兩種寫法有什麼差別嗎?

Image description

左邊的不會印出 10 ~ 19 度時的情況。
右邊的不會印出「很熱」,因為第一個條件 (溫度 >= 20) 就已經包括 (溫度 >= 30) 了,所以只有 20 度以上就是舒適,以下就是很冷。
請問下圖在「布林」這個變數是 true 和 false 的時候,分別會執行哪幾行的「做點什麼」?

Image description

布林為 true 時:5 -> 7 -> 11。
布林為 false 時: 7 -> 11。

 import Foundation

// 【ChaoCode】 Swift 基礎篇 9:條件式流程控制——If 實作作業

// 1. 請根據下述的學校入學條件判斷入學申請者能否入學,並印出對印的文字。
// 「這間學校的入學門檻是平均成績須達 80 分,學費為 128,000 元,平均成績達 92 分以上可以以獎學生身份享全額補助入學。」

func 入學申請(名字: String, 平均成績: Int, 存款: Int)
{
    //👇 請根據不同情況印出以下四種結果。
    if(平均成績>=92) {
        print("🎉 \(名字) 以獎學生身份入學。")
    }else if(平均成績>=80&&平均成績<92 && 存款>=128000)
    {print("✅ \(名字) 可以入學。")
    }
    else if(平均成績>=80&&平均成績<92 && 存款<128000)
    {print("😢 \(名字) 成績達入學門檻,但無法支付學費,不能入學。")
    }
    else if(平均成績<80){print("❌ \(名字) 成績未達入學門檻,不能入學。")
    }


}

入學申請(名字: "保羅", 平均成績: 80, 存款: 128000)
入學申請(名字: "傑克", 平均成績: 92, 存款: 18000)
入學申請(名字: "蘿拉", 平均成績: 95, 存款: 356700)
入學申請(名字: "馬丁", 平均成績: 70, 存款: 152340)
入學申請(名字: "米娜", 平均成績: 84, 存款: 238920)
入學申請(名字: "海倫", 平均成績: 90, 存款: 127000)


// 2. 請根據 Google Doc 中提供的體脂率計算法和體型對照表印出正確的體脂率和體型資訊,如果對象低於 18 歲則只印出「多吃多動,健康快樂長大就好」。

func 體脂率計算(名字: String, 是男生: Bool, 年齡: Int, 身高: Double, 體重: Double)
{
    //👇 請根據不同情況印出以下五種結果,記得要計算體脂率並把它印出來的值四捨五入到小數點第一位。
       if (年齡 < 18) {
           print("\(名字),多吃多動,健康快樂長大就好。")
       } else {
           let bmi = 體重 / ((身高 / 100) * (身高 / 100))
           let bodyFat = 1.2 * bmi + 0.23 * Double(年齡) - 5.4 - 10.8 * (是男生 ? 1 : 0)
           let formatter = NumberFormatter()
           formatter.maximumFractionDigits = 1
           let bodyFatString = "\(名字)的體脂率是 \(formatter.string(for: bodyFat)!) %"

           // 💡 以男生 18~39 歲為基準,計算不同性別和年紀的偏差值。
           let genderBias = 是男生 ? 0 : (21 - 8)

           let isYoungAdult = 年齡 < 40
           var ageBias = 0
           if (!isYoungAdult) {
               let isElder = 年齡 > 59
               if (是男生) { ageBias += isElder ? 5 : 3 }
               else { ageBias += isElder ? 3 : 2 }
           }

           // 💡 把偏差值加起來,計算出正常範圍和肥胖下限
           let bias = Double(genderBias + ageBias)
           let normalLowerBound  = 8 + bias
           let normalUpperBound  = 20 + bias
           let obesityLowerBound = 25 + bias
           let idealRangeString  = "理想值應為 \(normalLowerBound) % ~ \(normalUpperBound) %"

           if (bodyFat < normalLowerBound) {
               print("\(bodyFatString),低於正常標準,\(idealRangeString)。")
           } else if (bodyFat < normalUpperBound) {
               print("\(bodyFatString),位於正常標準,繼續保持!")
           } else if (bodyFat < obesityLowerBound) {
               print("\(bodyFatString),高於正常標準,\(idealRangeString)。")
           } else {
               print("\(bodyFatString),屬於過胖體型,\(idealRangeString)。")
           }
       }


}

體脂率計算(名字: "亞倫", 是男生: true, 年齡: 17, 身高: 149, 體重: 40)
體脂率計算(名字: "克里斯", 是男生: true, 年齡: 40, 身高: 150, 體重: 60)
體脂率計算(名字: "丹尼", 是男生: true, 年齡: 20, 身高: 181, 體重: 53)
體脂率計算(名字: "比爾", 是男生: true, 年齡: 26, 身高: 165, 體重: 53)
體脂率計算(名字: "吉米", 是男生: true, 年齡: 60, 身高: 173, 體重: 120)
體脂率計算(名字: "艾瑪", 是男生: false, 年齡: 17, 身高: 149, 體重: 40)
體脂率計算(名字: "凱西", 是男生: false, 年齡: 40, 身高: 150, 體重: 60)
體脂率計算(名字: "黛比", 是男生: false, 年齡: 20, 身高: 180, 體重: 53)
體脂率計算(名字: "瑪麗", 是男生: false, 年齡: 26, 身高: 165, 體重: 53)
體脂率計算(名字: "米蘭達", 是男生: false, 年齡: 60, 身高: 173, 體重: 120)


Enter fullscreen mode Exit fullscreen mode

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay