[英]Swift equivalent for while loops with else in python
我有以下用 Swift 编写的代码
if current_game_state == game_state.in_game {
while player_card_values.sum() <= 20 {
for touch: AnyObject in touches {
let point_of_touch = touch.location(in: self)
if hit_label.contains(point_of_touch){
spawn_card()
print("Hit Button Pressed")
}
}
}
else {
if self.player_card_values.sum() == 21 {
print("Player has received BJ!")
self.player_status = person_status.stand
break
}
}
for touch: AnyObject in touches {
let point_of_touch = touch.location(in: self)
if stand_label.contains(point_of_touch){
print("Stand Button Pressed")
player_status = person_status.stand
}
}
}
}
所有内部接触都始于 Spritekit。 但是出于某种原因,我在尝试编译它时遇到了两个错误。
没有while ... else ...
在 Swift 中,您将不得不重构您的逻辑。
此外,您不想在touchesBegan
内循环touchesBegan
您的应用程序将停止响应!
闭包表达式未使用,在它说 else 的那一行。 不知道为什么,因为该表达式未被使用,因为其中有代码。
在你的
while
块结束之后,你还有else
。 在 whereif
/else if
块结束后才有意义,而不是在while
之后。
未标记的中断只允许在循环或开关内,标记的中断需要退出和 if 或 do。
包含
break
else
块不是while
主体的一部分。
您需要重新组合您的代码,您在代码中放错了}
并且它对编译器没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.