循环和嵌套的if语句问题

motor = peripheral.wrap("right")

repeat

RPM = motor.getSpeed()
print("当前转速为", RPM, "RPM。需要多少RPM?")
setrpm = tonumber(io.read())
if type(setrpm) == "number"
    then
        motor.setSpeed(setrpm)
        goto contine

    else
        print("必须是在-256到256之间的数字")
        print("当前转速为", RPM, "RPM。需要多少RPM?")
        setrpm = tonumber(io.read())
        end
::continue::

rpm = motor.getSpeed()
su =  ( rpm * 16)
fe = motor.getEnergyConsumption()
print("您已经设置了转速为", rpm, "RPM")
print("当前扭力容量为", su, "SU")
print("当前能源消耗为", fe, "FE/t")

until( fe > 100 )
end

期望的行为 循环直到fe=100或更高

当前行为 motor.lua:12 '=' expected near 'continue'

在computercraft中编写一个循环代码来询问一个模块需要以哪个转速旋转,期望的行为是无限循环代码,直到FE>100(对于它正在查看的模块而言是不可能的)

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

点赞
stackoverflow用户2858170
stackoverflow用户2858170

Computercraft 使用 Lua 5.1。 goto 是 Lua 5.2 中引入的,因此您无法在脚本中使用它。

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

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

在执行完块A或B后,Lua 将自动继续执行 end 之后的代码。您不必显式地告诉 Lua 这样做。

在 repeat until 语句中,条件后不会有 end。正确的语法是:

repeat block until exp

在此发布您的问题之前,请至少检查拼写错误。contine 不是您打算转到的标签。

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

2021-11-02 20:38:38