Computercraft Lua循环嵌套if场景goto跳转报'=' expected near 'continue'错误
报错根因
- 核心语法错误来自
goto目标标签拼写不匹配:代码中跳转语句写为goto contine,比实际定义的::continue::标签少了字母u,Lua解析器无法识别为合法跳转指令,误以为你调用了不存在的continue关键字,因此抛出'=' expected near 'continue'错误。 - 额外存在的语法问题:代码末尾多余了
end关键字,repeat...until结构不需要额外的end闭合;终止条件fe > 100和你预期的fe≥100停止逻辑不符;else分支二次读取用户输入后未做校验,存在传入非法值的风险。
修正后完整代码
motor = peripheral.wrap("right") local fe = 0 -- 初始化fe变量避免首次循环判断报错 repeat local RPM = motor.getSpeed() print("current rpm is ", RPM, "RPM. What RPM do you need?") local setrpm = tonumber(io.read()) if type(setrpm) ~= "number" then print("Must be a number between -256 and 256") goto continue end -- 数值合法才执行设置和输出逻辑 motor.setSpeed(setrpm) local currentRpm = motor.getSpeed() local su = currentRpm * 16 fe = motor.getEnergyConsumption() print("You have set speed " , currentRpm, " RPM") print("Current stress cap is now ", su, "SU") print("Power Consumption is now ", fe, "FE/t") ::continue:: until(fe >= 100)
内容的提问来源于stack exchange,提问作者Jordan Bennett
相关产品推荐
相关产品推荐

