小王子與小狐狸,用 if else 述說一個動人故事 ~ 彼得潘的 iOS App 作業

Claire Chou
Jul 30, 2017 · 2 min read

把現成的小王子與小狐狸的橋段搬上來作為練習嚕

程式碼:

var firstName = “小王子”

var comeTimes = 0

var story = “ 對於小狐狸而言,你是”

if firstName == “小王子” {

story += “\(firstName),一個有特別緣分的人”

if comeTimes == 0 {

story += “,但是還是只是一個比較特別一點點的路人~”

}

else if comeTimes >= 1 && comeTimes <= 5 {

story += “,請你明天再同一個時間來,建立一個模式,這樣一來,比方說你下午四點鐘要來,那麼,從三點開始小狐狸就會有幸福的感覺,時間越接近越覺得幸福。”

}

else if comeTimes >= 6 {

story += “,你讓小狐狸的生命有如被陽光照耀般充滿希望.他可以認出你和別人不同的腳步聲,你的腳步聲就像優美的音樂一樣.金色的小麥都讓他想起你,因此他從此之後也喜歡聽吹過麥田的風聲了.”

}

}

else {

story += “擦肩而過的路人,當聽到你的腳步聲,他會立刻鑽到地底下。”

}

print(story)

目前的執行結果是

對於小狐狸而言,你是小王子,一個有特別緣分的人,但是還是只是一個比較特別一點點的路人~

如果更動comeTimes 或是firstName 結果會稍微不同

彼得潘的 Swift iOS App 開發教室

學習 Swift iOS App 開發的學生作品集

    Written by

    學習 Swift iOS App 開發的學生作品集

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade