0
以下代碼像紅綠燈一樣運行。紅色 - >黃色 - >綠色。我的代碼如何編寫,以便當「else if timerInt == 0 {」來自r.png或yellow.png時出現。基本上硬幣會翻轉,r或黃色都會出現。但機會是平等的,而且總是隨機的。謝謝如何創建隨機化的if else語句swift 3
timerInt -= 1
if timerInt == 2{
light.image = UIImage(named: "r.png")
} else if timerInt == 1 {
light.image = UIImage(named: "yellow.png")
} else if timerInt == 0 {
light.image = UIImage(named: "g.png")
}
我會寫這樣的:'light.image = UIImage(named:arc4random_uniform(2)== 0?「r」:「yellow」)' – rmaddy
\ _(ツ)_ /¯在生產代碼中,I 'd當然在資源命名空間中有這些圖像,所以更可能'light.image = arc4random_uniform(2)== 0? Resource.Image.redLight:Resource.Image.yellowLight'。但是,在這裏它可能是有道理的。 – tomahh