首页 > 解决方案 > 循环和嵌套 if 语句的问题

问题描述

motor = peripheral.wrap("right")

repeat 


RPM = motor.getSpeed()
print("current rpm is ", RPM, "RPM. What RPM do you need?")
setrpm = tonumber(io.read())
if type(setrpm) == "number"
    then
        motor.setSpeed(setrpm)
        goto contine

    else 
        print("Must be a number between -256 and 256")
        print("current rpm is ", RPM, "RPM. What RPM do you need?")
        setrpm = tonumber(io.read())
        end
::continue::

rpm = motor.getSpeed()
su =  ( rpm * 16)
fe = motor.getEnergyConsumption()
print("You have set speed " , rpm, " RPM")
print("Current stress cap is now ", su, "SU")
print("Power Consumption is now ", fe, "FE/t")

until( fe > 100 )
end

直到 fe=100 或更多的预期行为循环

当前行为 motor.lua:12 '=' 预计在“继续”附近

在计算机技术中编写一个代码循环来询问一个块需要以什么 rpm 旋转,预期的行为是不断循环代码直到 FE>100(对于块它看起来不可能)

标签: lua

解决方案


Computercraft 使用 Lua 5.1。goto是在 Lua 5.2 中引入的,所以你不能在你的脚本中使用它。

除此之外,像这样使用它是没有意义的。

if condition then
  -- block A
else
  -- block B
end

在执行块 A 或 B 之后 Lua 会自动在end. 您不必明确告诉 Lua 这样做。

在重复直到语句中end,条件之后没有。正确的语法是:

repeat block until exp

在此处发布您的问题之前,至少检查一下是否有错别字。contine不是您打算去的标签。

请参考https://www.lua.org/manual/5.1/manual.html


推荐阅读