lua - 循环和嵌套 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(对于块它看起来不可能)
解决方案
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
不是您打算去的标签。
推荐阅读
- sql - 连接中过滤器的 SQL 性能与结果中的 SQL 性能
- python - 字符串格式在 tkinter 中不起作用
- amazon-web-services - 15 分钟后 Lambda 执行超时我能做什么?
- sql - 查找三个工作日的平均值,按小时细分
- node.js - 为什么在 `npm install` 时`package-lock.json` 会导致 docker 容器构建失败?
- android - 在颤动中加载 api 时出现类型转换错误
- python - 随机词不返回
- graphviz - 时间线上的 Graphviz 节点
- c++ - Boost 字符串处理函数是否曾经持有全局锁?
- sql-server - 如何解决 SQL Server 2012 中的死锁错误?