Swift 系统学习 09 repeat while
来源:互联网 发布:淘宝卖家如何加入一淘 编辑:程序博客网 时间:2024/06/05 07:45
//: Playground - noun: a place where people can playimport UIKit//repeat {// //} while aValue == bValue// 课堂练习一: 使用repeat whilevar aWin = falsevar bWin = falserepeat { let aRandomValue = arc4random_uniform(6) + 1 let bRandomValue = arc4random_uniform(6) + 1 if aRandomValue > bRandomValue { aWin = true } else if aRandomValue < bRandomValue { bWin = true } else { print("Draw!") }} while !aWin && !bWin// 验证谁赢let finalWinner = aWin ? "A" : "B"print("\(finalWinner) wins in the first game!")// 课堂练习二:使用while死循环while true { let aRandomValue = arc4random_uniform(6) + 1 let bRandomValue = arc4random_uniform(6) + 1 if aRandomValue == bRandomValue { print("Draw!") continue } let winner = aRandomValue > bRandomValue ? "A" : "B" print("\(winner) wins in second game!") break}// 课堂练习三:let someValue = 10// 方式一: 使用区间运算符// Int.min获取Int类型的最小值Int.minswitch someValue {case Int.min..<0: print("The value is negative!")case 0: print("The value is zero!")default: print("The value is positive!")}// 方式二: 使用where模式匹配switch someValue {case let x where x < 0: print("The value is negative!")case let x where x == 0: print("The value is zero!")default: print("The value is positive")}Day02回顾:1. 常量和变量 const int imInt = 10; // 编译时已经知道整型 let imInt = 10 // 运行时才知道整型 var imInt = 10 // 多次初始化2. 基本数据类型: Int / Float / Double / String / Character / Bool —> 都是结构体(属性和方法)3. 元组: 使用逗号分隔, 使用小括号包含多个相同或者不同的数据的数据结构 3.1 获取元组分量: .0 .1 .2 ….. 3.2 给元组分量命名, 通过名字的方式获取分量的值4. 区间运算符: 和switch和for循环结合紧密 a. 闭区间 x…y b. 开区间 x..<y今天内容:·可选型Optional(***重点和难点)·控制流语句: if / else / for / while / repeat while·容器类: 数组和字典(**)可选型Optional1. 背景/为什么: C语言的main函数返回int, 如果返回0, 表明程序没有bug; 返回负值, 表明程序有错误. —> 需求: 给定字符串, 返回该字符串的整数值 定义函数: int convertIntValue(char *) { // 代码逻辑 } a. “123” -> 123 b. “hello” -> 返回什么值, 表示无法转成整型-9342856238462. 是什么? 描述: 新增的类型Optional, 用来控制值的存在性, 如果存在, 赋值某个类型的值; 如果不存在, 给nil —> Swift, 不可以个任意类型赋值为nil, 除了可选型Optional —> 无法单独存在, 和其他的类型结合使用 a. Int? 称为整型可选型; String? 字符串可选型..... b. var imInt: Int = 10 imInt = nil // 有编译错误 c. Swift语言: func convertIntValue(inputStr: String) -> Int? { // 如果转不成功, 返回nil // 如果能转成功, 返回整型的值 }3. 样例: 可选型的声明/使用/解包 [ 01_optional ] /05_Swift/Day02/Day02-AM1.zip4. 样例: 可选型的其他用法 [ 02_other_optional ] —> 可选型总结: a. 和其他类型结合使用: Int? String? Bool? ….. b. 存在: 对应类型有值; 不存在: nil c. 解包: 强制解包!; if let解包; nil聚合 ?? ; 链表达?.5. 样例: 控制语句control flow语句: if / else / switch / for / while / repeat while [ 03_control_flow ]/05_Swift/Day02/Day02-AM2.zip———————— 下午内容 ————1. 样例: switch语句的使用 [ 04_switch_statement ] 2. 样例: 数组(结构体)的使用 [ 05_array ] OC: NSMutableArray *mutableArray = [NSMutableArray new]; [mutableArray addObject: @10]; [mutableArray addObject: @“hello”]; NSArray *newArray = mutableArray; [mutableArray addObject: @1000]; C: int intOne = 10; int intTwo = intOne; intOne = 20;/05_Swift/Day02/Day02-PM1.zip3. 课堂练习一和二: [ 06_practice ] 3.1 需求: 两个人投色子, 两个人投掷的一样, 循环继续; 只要一个人投掷点数大于另一个人, 循环结束 3.2 要求一: repeat while(仿照C语言的do while循环, 把do关键词改成repeat)实现上述需求 3.3 要求二: while死循环实现上述需求(提示: break/continue)4. 课堂练习三: 4.1 将下面的if/else if语句改成switch语句 5. 复习可选型Optional: 将上午的可选型代码重新写一遍, 或者将官方文档的可选型代码重新写一遍./05_Swift/Day02/Day02-PM2.zipDay02重要知识点总结:1. 掌握可选型的基本语法; 了解可选型存在含义2. 掌握可选型的几种解包方式 a. 强制解包(风险) b. if let解包(可选型绑定Optional Binding) c. nil聚合(nil-coalescing)语法 d. 可选型链式表达(Optional Chaining)3. 掌握数组的创建和初始化4. 理解”值类型”和”引用类型”概念([05_array.playground])
0 0
- Swift 系统学习 09 repeat while
- Swift-(do/repeat)while循环
- swift控制语句,for,while,repeat-while,if,switch
- Swift之控制流( if 和 switch 、for-in 、 for 、 while 和 repeat-while)
- while和repeat
- mysql while loop repeat
- mysql 循环while、loop、REPEAT
- swift 学习笔记(14)-while 循环
- swift -- 系统学习资料
- 宏的扩展——REPEAT/WHILE
- 宏的扩展——REPEAT/WHILE
- lua中的三种循坏(while,for,repeat)
- R中的程序结构(for,while,repeat)
- swift while、do-while循环
- Swift 系统学习 15 枚举
- 【Swift】学习笔记(五)——控制语句(if,switch,for-in,for,while,do-while)
- Swift 循环for&while
- swift 循环 for while
- Xcode使用Profile的leaks检测内存泄露
- 求两个数的最大公约数的Java实现
- java开发常用在线工具整理
- 在线预览PDF
- css3实现固定表格头部而无需设置单元格td的宽度
- Swift 系统学习 09 repeat while
- vim编辑文档误用Ctrl+S导致vim假死
- QThread 子类化 打开相机
- 初识Linux
- 1070. Mooncake 解析
- Android进阶——MVP从入门到进阶
- js数组中的对象去重
- 整型和二进制之间的互相转换(代码)
- 一段总结--软件设计性能指标