美文网首页
swift-给语句打标签

swift-给语句打标签

作者: g_h_j | 来源:发表于2020-06-11 16:05 被阅读0次

    ///给语句打标签 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!")

    }

    相关文章

      网友评论

          本文标题:swift-给语句打标签

          本文链接:https://www.haomeiwen.com/subject/neibtktx.html