使用亂數決定今天禮拜幾
Published in
Mar 27, 2022
小時候最常說的猴子繞口令
星期一,猴子…..
星期二,猴子……
現在把星期幾亂數化了
@IBAction func Change(_ sender: Any) {
let number = Int.random(in: 0...6)
changeMonkey(number: number)
}
使用亂數後,帶入到changeMonkey內
func changeMonkey(number: Int){
WeekLabel.text = newWeekLabel[number]
whatLabel.text = newWhatLabel[number]
monkeyImage.image = UIImage(named: newImage[number])
}
會把星期給帶入到陣列內
var newImage = ["Monday","Tuesday","Wednesday","Thursday","Friday"
,"Saturday","Sunday"]
var newWeekLabel = ["星期一,",
"星期二,",
"星期三,",
"星期四,",
"星期五,",
"星期六,",
"星期日,"]
var newWhatLabel = ["猴子穿新衣。",
"猴子肚子餓。",
"猴子去爬山。",
"猴子去考試。",
"猴子去跳舞。",
"猴子去斗六。",
"猴子過生日。"]
而我的習慣是會把這些資訊用陣列包起來,才不用用if..else判斷
雖然功能很簡陋,但是是我第一次使用亂數的功能…
Demo
附上Github