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