5.拓展技能
挑戰(zhàn):將重復(fù)模式分解成函數(shù)和for循環(huán)并淋。
你已經(jīng)學(xué)習(xí)了編程的基礎(chǔ)知識(shí),并通過你的角色破解了大量關(guān)卡渴杆。既然已經(jīng)了解了命令又跛、函數(shù)和for循環(huán),那么你一定已準(zhǔn)備好使用各項(xiàng)技能來完成這項(xiàng)挑戰(zhàn)了!
在關(guān)卡世界中一共有三段階梯,并且每段階梯上都有同樣一組任務(wù)需要執(zhí)行傲武。你能看出這些任務(wù)的模式并寫出代碼嗎?
func myToggle(){
? ? moveForward()
? ? moveForward()
? ? turnRight()
? ? for i in 1 ..7{
? ? ? ? moveForward()
? ? }
? ? toggleSwitch()
? ? turnLeft()
? ? turnLeft()
? ? for i in 1...7{
? ? ? ? moveForward()
? ? }
? ? turnRight()
}
for i in 1...3 {
? ? myToggle()
}