如果陈述不适用于Lua

Jea*_*Luc 1 lua

这是我的代码.我正在玩tekkit并希望控制水流量.棕色电线上有红石电源但不是黑色电源,但它仍然适用于ERROR!任何人都知道我的问题是什么?

在Lua代码中:

shell.run("clear")
brown = rs.testBundledInput("back", colors.brown)
black = rs.testBundledInput("back", colors.black)
t = true
f = nil

if brown == t and black == f then
  redstone.setBundledOutput("back", restone.getBundledOutput("back") -colors.brown)
  print("All water is flowing.")
  sleep(3)
  shell.run("2")
elseif brown == f and black == t then
  redstone.setBundledOutput("back", restone.getBundledOutput("back") -colors.black)
  print("All water is flowing.")
  sleep(3)
  shell.run("2")
elseif brown == t and black == t then
  redstone.setBundledOutput("back", restone.getBundledOutput("back") -colors.brown)
  redstone.setBundledOutput("back", restone.getBundledOutput("back") -colors.black)
  print("All water is flowing.")
  sleep(3)
  shell.run("2")
elseif brown == f and black == f then
  print("All water is flowing.")
  sleep(3)
  shell.run("2")
else
  print("ERROR!")
end
Run Code Online (Sandbox Code Playgroud)

Yu *_*Hao 7

从代码中,我猜测brown并且black是布尔类型,它们是true或者false.但是你要比较它们:

t = true
f = nil 
Run Code Online (Sandbox Code Playgroud)

这是不正确的,因为虽然双方falsenil都是假的价值观,他们是不一样的,即,false不等于nil.所以改成它f = false.

但是,这有点多余,你不需要tf在if语句中.当你使用:

if brown == t and black == f then
Run Code Online (Sandbox Code Playgroud)

您可以使用此方法测试它们:

if brown and not black then
Run Code Online (Sandbox Code Playgroud)