2013-08-03 68 views
1

这是我的代码。我正在玩Tekkit,想要控制水流量。在棕色电线上有红石电源,但不是黑色,但它仍然会到ERROR!任何人都知道我的问题是什么?如果语句不工作在Lua

在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 
+2

正确的缩进会使你的代码*多*易于阅读。 –

+2

对不起,那个人..但t =真,f =零?为什么?认真的原因? – starmole

+0

同样适用于全球的“棕色”和“黑色”。他们应该可能是“本地”。参见[Lua_中的程序设计](http://www.lua.org/pil/4.2.html)。 –

回答

7

从代码,我猜brownblack是布尔类型,它们要么truefalse。但你是他们比较:

t = true 
f = nil 

这是不正确的,因为虽然双方falsenil都是假的值,它们是不一样的,即,false不等于nil。所以将其更改为f = false

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

if brown == t and black == f then 

你可以用这个来代替对其进行测试:

if brown and not black then 
+0

几分钟后,我不停地看着问题中的''back'',并想知道为什么在答案中使用* brown *和* black *。 : -/ – hjpotter92

+0

友好的回答。谢谢你!但对我而言,这个问题只是创造一个新的问题:为什么有人会这样做?这并不容易,更没有意义..必须有一个原因?或者至少有一个故事? – starmole

+0

@starmole我不是在追问你的问题,你在“为什么会有人这样做”中指“这个”是什么意思,你的意思是“如果棕色而不是黑色”这样的陈述? –