MacOS Playgrounds 学习编程一 第十九关 拓展技能

还记得原来你学过的函数吗?循环学了这么久,还没有用上它啊。

我们现在就把函数的本领发挥出来。

看一下我们的任务,就是要把三个开关都打开。

当我们仔细看一下,这三个开关前的路径都是一样的。那就意味着重要的事情做三遍。

我们可以通过一个函数来把共同的任务提汲出来,封装成一个命令,把它嵌入循环里。

这个过程自己总结一下。

我们把代码写在下面:

func Forward7() {  //定义函数

    moveForward()

    moveForward()

    moveForward()

    moveForward()

    moveForward()

    moveForward()

    moveForward()

    toggleSwitch()

    turnRight()

    turnRight()

}

for i in 1 ... 3 {   //定义循环

    moveForward()

    moveForward()

    turnRight()

    Forward7()

    Forward7()

    turnLeft()

}

在这里我们要养成一个写程序注解的习惯了。以方便我们以后查看。如果三个月不看这段代码,三个月后,你看自己写的跟看别人写的是一样的。注解是越详细越好。

这一关的实现有好几种方法,其实也就是可以用多个函数实现,你可以试一下。但是目标不变。

传送门:

MacOS Playgrounds学习 学习编程一 前记--https://www.jianshu.com/p/a0de277374a4

MacOS Playgrounds 学习编程一 第一关 发出命令--https://www.jianshu.com/p/3c85397f1eae

MacOS Playgrounds 学习编程一 第二关 添加新命令--https://www.jianshu.com/p/4efa5c18dc50

MacOS Playgrounds 学习编程一 第三关 切换开关--https://www.jianshu.com/p/00556d6430bf

MacOS Playgrounds 学习编程一 第四关 传送门练习--https://www.jianshu.com/p/47bd57d0193f

MacOS Playgrounds 学习编程一 第五关 发现并修复程序错误--https://www.jianshu.com/p/5bc9200edbbe

MacOS Playgrounds 学习编程一 第六关 消灭程序错误练习--https://www.jianshu.com/p/f19bd2955ed8

MacOS Playgrounds 学习编程一 第七关 最短路线--https://www.jianshu.com/p/1ef210253be5

MacOS Playgrounds 学习编程一 第八关 组合新行为--https://www.jianshu.com/p/f548b1fd3134

MacOS Playgrounds 学习编程一 第九关 创建新函数--https://www.jianshu.com/p/22ab39aaea97

MacOS Playgrounds 学习编程一 第十关 收集、切换、重复--https://www.jianshu.com/p/e43d60f98bc7

MacOS Playgrounds 学习编程一 第十二关 嵌套模式--https://www.jianshu.com/p/645f6716217c

MacOS Playgrounds 学习编程一 第十三关 嵌入式阶梯--https://www.jianshu.com/p/d87f9662601d

MacOS Playgrounds 学习编程一 第十四关 寻宝--https://www.jianshu.com/p/18c73154e46b

MacOS Playgrounds 学习编程一 第十五关 使用循环--https://www.jianshu.com/p/f7c0866930ec

MacOS Playgrounds 学习编程一 第十六关 循环每一侧--https://www.jianshu.com/p/94d75e9d5eab

MacOS Playgrounds 学习编程一 第十七关 行至边缘再返回--https://www.jianshu.com/p/cc7e8095bb9a

MacOS Playgrounds 学习编程一 第十八关 循环跳跃者--https://www.jianshu.com/p/99618bef896e

MacOS Playgrounds 学习编程一 第十九关 拓展技能--https://www.jianshu.com/p/a20810b11d6d

MacOS Playgrounds 学习编程一 第二十关 宝石农场--https://www.jianshu.com/p/8c3808d1a646

MacOS Playgrounds 学习编程一 第二十一关 席卷四处--https://www.jianshu.com/p/cc7f6a1c34e7

MacOS Playgrounds 学习编程一 第二十二关 检查开关--https://www.jianshu.com/p/e34fb0431b6e

MacOS Playgrounds 学习编程一 第二十三关 使用 else if--https://www.jianshu.com/p/6414ef7692e2

MacOS Playgrounds 学习编程一 第二十四关 循环条件代码--https://www.jianshu.com/p/a657e47b851d

MacOS Playgrounds 学习编程一 第二十五关 满足条件时向上爬--https://www.jianshu.com/p/4a69a1c3435f

MacOS Playgrounds 学习编程一 第二十六关 定义更巧妙的函数--https://www.jianshu.com/p/d61e03e095ae

MacOS Playgrounds 学习编程一 第二十七关 围困其中--https://www.jianshu.com/p/88442c7ce61b

MacOS Playgrounds 学习编程一 第二十八关 决策树--https://www.jianshu.com/p/0843bd9ae286

MacOS Playgrounds 学习编程一 第二十九关 使用“非”运算符--https://www.jianshu.com/p/be8fa74b6c46

MacOS Playgrounds 学习编程一 第三十关 非之螺旋--https://www.jianshu.com/p/f42905cbaed1

MacOS Playgrounds 学习编程一 第三十一关 检查这个与那个--https://www.jianshu.com/p/67b1c6e16b6a

MacOS Playgrounds 学习编程一 第三十二关 检查这个或那个--https://www.jianshu.com/p/d94a67728a93

MacOS Playgrounds 学习编程一 第三十三关 逻辑迷宫--https://www.jianshu.com/p/62bbc0ce2490

MacOS Playgrounds 学习编程一 第三十四关 当……时运行代码--https://www.jianshu.com/p/9aa41733c6d0

MacOS Playgrounds 学习编程一 第三十五关 创建更巧妙的while循环--https://www.jianshu.com/p/3fdfb33a443f

MacOS Playgrounds 学习编程一 第三十六关 正确选取工具--https://www.jianshu.com/p/e5ed25be735e

MacOS Playgrounds 学习编程一 第三十七关 四乘以四--https://www.jianshu.com/p/81ed2e6009d4

MacOS Playgrounds 学习编程一 第三十八关 转身--https://www.jianshu.com/p/a3c9bae7d6a2

MacOS Playgrounds 学习编程一 第三十九关 富饶之地--https://www.jianshu.com/p/077743cdfcdd

MacOS Playgrounds 学习编程一 第四十关 套嵌循环--https://www.jianshu.com/p/d8670c9e7962

MacOS Playgrounds 学习编程一 第四十一关 随机矩形--https://www.jianshu.com/p/61b916f61e37

MacOS Playgrounds 学习编程一 第四十二关 始终右转--https://www.jianshu.com/p/b7cf92fbfcaa

MacOS Playgrounds 学习编程一 第四十三关 右手定则--https://www.jianshu.com/p/9ccd20151c96

MacOS Playgrounds 学习编程一 第四十四关 调整算法--https://www.jianshu.com/p/10c2a0de990d

MacOS Playgrounds 学习编程一 第四十五关 征服迷宫--https://www.jianshu.com/p/b4d731fe3639

MacOS Playgrounds 学习编程一 第四十六关 左转还是右转--https://www.jianshu.com/p/cd7fa32316e5

MacOS Playgrounds 学习编程一 第四十七关 向左走,向右走--https://www.jianshu.com/p/b9b1dc9c8f2e

MacOS Playgrounds学习 学习编程一 小结后记--https://www.jianshu.com/p/8ad6485e447a

你可能感兴趣的:(MacOS Playgrounds 学习编程一 第十九关 拓展技能)