Lua中的“if then”语句出现错误(应该有一个“end”来关闭第28行的“if”语句)。

我收到了一个错误信息,说

bios:14: [string "Lighting"]:58: 'end' expected (to close 'if' at line 28)

我实在不知道自己在做什么,因为我是新手,对 Lua 和编程一窍不通。我猜想问题与某个地方没有 end 相关。

term.clear()
term.setCursorPos(17, 4)
print("欢迎")
sleep(2)
term.setCursorPos(8, 5)
print("你想控制哪个灯?")
input = read()
if input == "大厅" then
  term.clear()
  term.setCursorPos(17,4)
  print("开启还是关闭?")
  input = read()
  if input == "开启" then
    redstone.setOutput("back", true)
    print("大厅灯已开启")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("back", false)
    print("大厅灯已关闭")
    sleep(5)
    shell.run("Lighting")
  if input == "卧室" then
  term.clear()
  term.setCursorPos(17,4)
  print("开启还是关闭?")
  input = read()
  if input == "开启" then
    redstone.setOutput("left", true)
    print("卧室灯已开启")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("left", false)
    print("卧室灯已关闭")
    sleep(5)
    shell.run("Lighting")
  if input == "实验室" then
  term.clear()
  term.setCursorPos(17,4)
  print("开启还是关闭?")
  input = read()
  if input == "开启" then
    redstone.setOutput("right", true)
    print("实验室灯已开启")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("right", false)
    print("实验室灯已关闭")
    sleep(5)
    shell.run("Lighting")
  end
else
  print("错误")
  sleep(3)
  shell.run("Lighting")
end
点赞
用户4694621
用户4694621

看起来你在几处缺少了 end 词。

结构应该是这样的:

if .. then
  一些代码
else
  一些可选的代码
end

另外,尝试更好地缩进你的代码。那么你就可以清楚地知道你应该放 end 词。

你想要的可能是这样的:

term.clear()
...
input = read()

if input == "Hall" then
  ...
  if input == "on" then
    ...
  else
    redstone.setOutput("back", false)

    shell.run("Lighting")
  end -- 缺少 end!
end -- 缺少 end!

if input == "Bedroom" then
    ...
  if input == "on" then
    ...
  else
    redstone.setOutput("left", false)
    ...
    shell.run("Lighting")
  end -- 缺少 end!
end -- 缺少 end!

...
2016-01-18 22:50:47