大佬教程收集整理的这篇文章主要介绍了swift2 控制流,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
let numberOfLegs = ["spider": 8,"ant": 6,"cat": 4] for (animalName,legCount) in numberOfLegs { print("\(animalName)s have \(legCount) legs") } // spiders have 8 legs // ants have 6 legs // cats have 4 legs
for var index = 0; index < 3; ++index { print("index is \(indeX)") } // index is 0 // index is 1 // index is 2
var square = 0 var DiceRoll = 0 while square < finalSquare { // 掷骰子 if ++DiceRoll == 7 { DiceRoll = 1 } // 根据点数移动 square += DiceRoll if square < board.count { // 如果玩家还在棋盘上,顺着梯子爬上去或者顺着蛇滑下去 square += board[square] } } print("Game over!")
repeat { // 顺着梯子爬上去或者顺着蛇滑下去 square += board[square] // 掷骰子 if ++DiceRoll == 7 { DiceRoll = 1 } // 根据点数移动 square += DiceRoll } while square < finalSquare print("Game over!")
let temperatureInFahrenheit = 90 if temperatureInFahrenheit <= 32 { print("It's very cold. Consider wearing a scarf.") } else if temperatureInFahrenheit >= 86 { print("It's really warm. Don't forget to wear sunscreen.") } else { print("It's not that cold. Wear a t-shirt.") } // 输出 "It's really warm. Don't forget to wear sunscreen."
let someCharacter: Character = "e" switch someCharacter { case "a","e","i","o","u": print("\(someCharacter) is a vowel") case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z": print("\(someCharacter) is a consonant") default: print("\(someCharacter) is not a vowel or a consonant") } // 输出 "e is a vowel"
let count = 3_000_000_000_000 let countedThings = "stars in the Milky Way" var naturalCount: String switch count { case 0: naturalCount = "no" case 1...3: naturalCount = "a few" case 4...9: naturalCount = "several" case 10...99: naturalCount = "tens of" case 100...999: naturalCount = "@R_732_4892@ of" case 1000...999_999: naturalCount = "thousands of" default: naturalCount = "millions and millions of" } print("There are \(naturalCount) \(countedThings).") // 输出 "There are millions and millions of stars in the Milky Way."
let somePoint = (1,1) switch somePoint { case (0,0): print("(0,0) is at the origin") case (_,0): print("(\(somePoint.0),0) is on the x-axis") case (0,_): print("(0,\(somePoint.1)) is on the y-axis") case (-2...2,-2...2): print("(\(somePoint.0),\(somePoint.1)) is inside the Box") default: print("(\(somePoint.0),\(somePoint.1)) is outside of the Box") } // 输出 "(1,1) is inside the Box"
let anotherPoint = (2,0) switch anotherPoint { case (let x,0): print("on the x-axis with an x value of \(X)") case (0,let y): print("on the y-axis with a y value of \(y)") case let (x,y): print("somewhere else at (\(X),\(y))") } // 输出 "on the x-axis with an x value of 2"
let yetAnotherPoint = (1,-1) switch yetAnotherPoint { case let (x,y) where x == y: print("(\(X),\(y)) is on the line x == y") case let (x,y) where x == -y: print("(\(X),\(y)) is on the line x == -y") case let (x,y): print("\(X),\(y)) is just some arbitrary point") } // 输出 "(1,-1) is on the line x == -y"
let IntegerToDescribe = 5 var description = "the number \(IntegerToDescribE) is" switch IntegerToDescribe { case 2,3,5,7,11,13,17,19: description += " a prime number,and also" fallthrough default: description += " an Integer." } print(description) // 输出 "the number 5 is a prime number,and also an Integer."
gameLoop: while square != finalSquare { if ++DiceRoll == 7 { DiceRoll = 1 } switch square + DiceRoll { case finalSquare: // 到达最后一个方块,游戏结束 break gameLoop case let newSquare where newSquare > finalSquare: // 超出最后一个方块,再掷一次骰子 conTinue gameLoop default: // 本次移动有效 square += DiceRoll square += board[square] } } print("Game over!")
以上是大佬教程为你收集整理的swift2 控制流全部内容,希望文章能够帮你解决swift2 控制流所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。