码迷,mamicode.com
首页 > 编程语言 > 详细

Swift - 25 - 控制转移和二维数组

时间:2015-12-16 19:07:19      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

//: Playground - noun: a place where people can play

import UIKit

// fallthrough
// fallthrough会在当前case执行完之后继续下一个case
// 如果在下一个case中声明了变量, 则不能使用fallthrough
var coordinate = (1, 0)
switch coordinate {
case (0, 0):
    print("It‘s at origin!")
    fallthrough
case (_, 0):
    print("(\(coordinate.0), 0) is on the x-axis.")
    fallthrough
case (0, _):
    print("(0, \(coordinate.1)) is on the y-axis.")
    fallthrough
case (-2...2, 0...2):
    print("the coordinate is (\(coordinate.0), \(coordinate.1))")
default:
    print("(\(coordinate.0), \(coordinate.1)) is just an ordinary coordinate")
}

// break
for var i = 0; i < 10; i++ {
    print(i)
    for var j = 0; j < 10; j++ {
        print(j)
        if i + j == 10 {
            print(i + j)
            break   // 跳出break所在的循环体
        }
    }
}


// continue
var str = "askdjfskldjfdifjhget,n,mcnmvxcvawperiuzxncv,."
var num = 0;
for string in str.characters {
    switch string {
        case ",", ".":
            num++
        default:
            continue;
    }
}

// 初始化二维数组
var board = Array<Array<Int>>()
for i in 0..<10 {
    // count: 数组初始化后的元素个数 repeatedValue: 数组的元素的值
    board.append(Array(count: 10, repeatedValue: 0))
}

let randx = Int(arc4random()%10)
let randy = Int(arc4random()%10)
board[randx][randy] = 1
board

// mainloop用于标记某个循环, 有了标记, break就可以直接跳出某个指定的循环了. eg:
var i = 0, j = 0
mainFor:for i = 0; i < 10; i++ {   // mainFor这个标记是可以自定义的
    for j = 0; j < 10; j++ {
        if board[i][j] == 1 {
            break mainFor
        }
    }
}

print("board[\(i)][\(j)] = 1")

  

Swift - 25 - 控制转移和二维数组

标签:

原文地址:http://www.cnblogs.com/Rinpe/p/5051845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!