swift 函数中的嵌套 if 语句
nested if statement in swift function
以下函数从红灯到绿灯倒计时,然后计算绿灯亮起后用户按下按钮的反应时间。
func updateCounter() {
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: arc4random_uniform(2) == 0 ? "no.png" : "g.png")
timer.invalidate()
startStop.isEnabled = true
scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
}
}
其中指出 else if timerInt == 0
,为用户提供了一个随机函数。图像将变为绿色或显示 "x"。
如果显示 x,我希望用户必须按下表示游戏结束的按钮才能重新启动红灯序列。
如果显示绿灯,我希望用户必须测试他们的反应时间。
这就是函数现在已经 运行 的方式,只是显示 x 时它不会改变。我想我想要 运行 的函数如下:
if timeInt == 0 and green light is chosen
then run test reaction time
else if timeInt == 0 and x is chosen
then end reaction time and run game over button
我怎样才能做到这一点?
我不太清楚你想要达到什么目的,所以我尽力了。试试这个:
func updateCounter() {
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 {
let green = (arc4random_uniform(2) == 0)
light.image = UIImage(named: (green ? "g.png" : "no.png"))
if green {
// run test reaction time
} else {
//end reaction time and run game over button
}
// NOTE: Do you mean `scoreTimer.invalidate()`?
timer.invalidate()
startStop.isEnabled = true
// NOTE: This time interval seems WAY too small ↓↓↓↓↓↓: it is only a millisecond!
scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
}
}
用适当的代码替换前两条注释(即以 //
开头的行),并记下第三条和第四条注释。
以下函数从红灯到绿灯倒计时,然后计算绿灯亮起后用户按下按钮的反应时间。
func updateCounter() {
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: arc4random_uniform(2) == 0 ? "no.png" : "g.png")
timer.invalidate()
startStop.isEnabled = true
scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
}
}
其中指出 else if timerInt == 0
,为用户提供了一个随机函数。图像将变为绿色或显示 "x"。
如果显示 x,我希望用户必须按下表示游戏结束的按钮才能重新启动红灯序列。
如果显示绿灯,我希望用户必须测试他们的反应时间。
这就是函数现在已经 运行 的方式,只是显示 x 时它不会改变。我想我想要 运行 的函数如下:
if timeInt == 0 and green light is chosen
then run test reaction timeelse if timeInt == 0 and x is chosen
then end reaction time and run game over button
我怎样才能做到这一点?
我不太清楚你想要达到什么目的,所以我尽力了。试试这个:
func updateCounter() {
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 {
let green = (arc4random_uniform(2) == 0)
light.image = UIImage(named: (green ? "g.png" : "no.png"))
if green {
// run test reaction time
} else {
//end reaction time and run game over button
}
// NOTE: Do you mean `scoreTimer.invalidate()`?
timer.invalidate()
startStop.isEnabled = true
// NOTE: This time interval seems WAY too small ↓↓↓↓↓↓: it is only a millisecond!
scoreTimer = Timer.scheduledTimer(timeInterval: 0.0001, target: self, selector: #selector(ViewController.updateScoreTime), userInfo: nil, repeats: true)
}
}
用适当的代码替换前两条注释(即以 //
开头的行),并记下第三条和第四条注释。