///给语句打标签 gameLoop,结合break和continue使用
func test(){
letfinalSquare =25
varboard = [Int](repeating:0, count:finalSquare+1)
board[3] =8
board[6] =11
board[9] =9
board[10] =2
board[14] =10
board[19] =-11
board[22] =-2
board[24] =-8
varsquare =0
vardiceRoll =0
gameLoop:whilesquare != finalSquare{
diceRoll = Int(arc4random()%6+1)
print("掷骰子:\(diceRoll)")
switchsquare+diceRoll {
casefinalSquare:
print("到末尾了")
breakgameLoop
caseletnewSquarewherenewSquare>finalSquare:
print("超出范围了:\(newSquare),重新掷骰子")
continuegameLoop
default:
square += diceRoll
print("跳到:\(square)")
letnextStep = board[square]
ifnextStep>0{
square += nextStep
print("继续往前跳:\(nextStep),到\(square)")
}elseifnextStep<0{
square += nextStep
print("需要往后跳:\(-nextStep),到\(square)")
}
}
}
print("game over!")
}
网友评论