英文:
i tried searching repeat until loops and i only found repeat statement until condition
问题
如何制作成功的重复和直到循环?我的代码哪里出错了?
我做了
重复
向前移动()
直到(tick{2}
)
我尝试了
重复
向前移动()
直到{2}
)
这是实际结果:
角色只侧向移动了1格,而不是每2秒移动1格。
这是我期望的结果:
角色每2秒侧向移动。
<details>
<summary>英文:</summary>
how do I make a successful repeat and until loop? what did i do wrong in my code?
I did
repeat
move forward()
until(tick{2}
)
I tried
repeat
move forward()
until{2}
)
here's the actual result:
the character moved sideways only 1 space once not one space every 2 seconds.
here's what i was expecting:
the character moved sideways every 2 seconds
</details>
# 答案1
**得分**: 1
Lua中的```repeat```具有简单的语法
```Lua
-- repeat.lua
local i = 0 -- 让我们用这个
local s = 1 -- 可以更改的步长
repeat
if i == 0 then -- 仅在开始时输出
print('开始重复')
end
i = i + s -- 增加
-- 对i做一些操作
print(type(i), i, i * i, i + i, ((i * i + i) / 2), math.rad(i))
if i > 200 then -- 中断条件
print('重复停止') -- 中断前的输出
break -- 在这里离开循环
end
s = s + i -- 增加步长
until i == 420 -- 永远不会到达(因为有中断条件)
以上的输出
开始重复
number 1 1 2 1 0.017453292519943
number 3 9 6 6 0.05235987755983
number 8 64 16 36 0.13962634015955
number 21 441 42 231 0.36651914291881
number 55 3025 110 1540 0.95993108859688
number 144 20736 288 10440 2.5132741228718
number 377 142129 754 71253 6.5798912800186
重复停止
好的,最简单的方法是使用一个返回true
或false
的重复函数,如下所示...
-- 当函数返回true时停止
repeat until function_that_returns_false_or_true()
或者
-- 当函数返回false时停止
repeat until not function_that_returns_false_or_true()
英文:
Lua repeat
has a simple Syntax
-- repeat.lua
local i = 0 -- Lets work with this
local s = 1 -- Stepsize that can be changed
repeat
if i == 0 then -- Output only at Start
print('Start to Repeat')
end
i = i + s -- Increment
-- Doing something with i
print(type(i), i, i * i, i + i, ((i * i + i) / 2), math.rad(i))
if i > 200 then -- Break Condition
print('Repeat stopped') -- Output before break
break -- Leave the Loop here
end
s = s + i -- Increment the Stepsize
until i == 420 -- Never reached (Because of Break Condition)
Output of above
Start to Repeat
number 1 1 2 1 0.017453292519943
number 3 9 6 6 0.05235987755983
number 8 64 16 36 0.13962634015955
number 21 441 42 231 0.36651914291881
number 55 3025 110 1540 0.95993108859688
number 144 20736 288 10440 2.5132741228718
number 377 142129 754 71253 6.5798912800186
Repeat stopped
OK - Simpliest is to have a Function to repeat that returns either true
or false
than...
-- Stopping when Function returns true
repeat until function_that_returns_false_or_true()
Or
-- Stopping when function returns false
repeat until not function_that_returns_false_or_true()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论