Q: 尝试在lua中制作一个计数器

我完全是新手,正在尝试制作一个计时器,它可以计数然后停止。我已经编写了一个函数计时器来完成大部分工作,但是它一直在计数而没有停止。我该如何让它在50或100处停止呢?

startTime = os.time()
count = 1
while 1 do
        if os.difftime(os.time(), startTime) == 1 then
                print (count)
                count = count + 1
                startTime = os.time()
        end
end

原文链接 https://stackoverflow.com/questions/70215735

点赞
stackoverflow用户2858170
stackoverflow用户2858170

你可以使用 while 循环条件如下:

while count <= 50 do
-- 你的代码
end

或者使用 repeat 循环条件如下:

repeat
  -- 你的代码
until count == 50

或者你可以使用 break 关键字提前跳出任何循环:

if count == 50 then
  break
end
2021-12-03 14:42:31