Java教程

Playground 学习编程1-函数

本文主要是介绍Playground 学习编程1-函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

第二章|函数

    • 1. 组合新行为
    • 2. 创建新函数
    • 3. 收集、切换、重复
    • 4. 尽收囊中
    • 5. 嵌套模式
    • 6. 嵌入式阶梯
    • 7. 寻宝

1. 组合新行为

moveForward()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
moveForward()
collectGem()

2. 创建新函数

func turnRight() {
	turnLeft()
    turnLeft()
    turnLeft()
}
moveForward()
turnLeft()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnLeft()
moveForward()
toggleSwitch()

3. 收集、切换、重复

func nextFunc() {
	moveForward()
    collectGem()
    moveForward()
    toggleSwitch()
}
nextFunc()
moveForward()
turnLeft()
nextFunc()
moveForward()
moveForward()
turnLeft()
nextFunc()
moveForward()
turnLeft()
nextFunc()

4. 尽收囊中

func funcNext() {
	moveForward()
    collectGem()
}
collectGem()
funcNext()
funcNext()
turnRight()
funcNext()
funcNext()
turnRight()
funcNext()
funcNext()
turnRight()
funcNext()
turnRight()
funcNext()

5. 嵌套模式

func turnAround() {
	turnLeft()
    turnLeft()
}
func solveStair() {
	turnAround()
    moveForward()
    moveForward()
    collectGem()
}
moveForward()
collectGem()
solveStair()
turnAround()
moveForward()
turnLeft()
moveForward()
collectGem()
solveStair()

6. 嵌入式阶梯

func collectGemTurnAround() {
	moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
}
func solveRow() {
	turnRight()
    moveForward()
    turnLeft()
    collectGemTurnAround()
    collectGemTurnAround()
}
collectGemTurnAround()
collectGemTurnAround()
solveRow()
solveRow()

7. 寻宝

func turnAround(){
    turnLeft()
    turnLeft()
}
func oneToggle(){
    moveForward()
    moveForward()
    toggleSwitch()
}
func oneMove(){
    turnAround()
    moveForward()
    moveForward()
}

func twoToggle(){
    oneToggle()
    oneToggle()
    oneMove()
    moveForward()
    moveForward()
}
oneToggle()
oneMove()
oneToggle()
oneMove()
turnRight()
twoToggle()
twoToggle()
这篇关于Playground 学习编程1-函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!