检查计算机工艺 Lua 代码以操作门

我正在制作一个带有一些自定义 mod 的金库,我正在使用计算机来控制门,但我只能打开门而不能关闭。这段代码正确吗?

term.setTextColor(colors.yellow)
print("Vault-Tec Door Computer")
term.setTextColor(colors.white)
print("你想做什么?")
term.setTextColor(colors.blue)
print("Vault.打开")
print("Vault.关闭")
print("")
term.setTextColor(colors.white)
io.write("Vault-Tec:")
io.close()
    if io.read()=="Vault.Open" then
        term.setTextColor(colors.red)
        print("保险库的门正在打开,请远离")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 0)
        sleep(5)
    end
    if io.read()=="Vault.Close" then
        term.setTextColor(colors.red)
        print("保险库的门正在关闭,请远离")
        term.setTextColor(colors.white)
        redstone.setAnalogOutput("bottom", 15)
        sleep(5)
    end
点赞
用户5525442
用户5525442

你的第一个if语句调用了 io.read(),读入并比较用户输入的值与 Vault.Open。接下来的if语句会读取用户输入的下一个值并比较它与 Vault.Close。你只需要读取输入一次并将其存储在变量中,然后就可以在多个地方使用这个值了。

.....
local vaultStatus = io.read()
if vaultStatus == "Vault.Open" then
    term.setTextColor(colors.red)
    print("保险库门开启,请远离")
    term.setTextColor(colors.white)
    redstone.setAnalogOutput("bottom", 0)
    sleep(5)
end
if vaultStatus == "Vault.Close" then
    term.setTextColor(colors.red)
    print("保险库门关闭,请远离")
    term.setTextColor(colors.white)
    redstone.setAnalogOutput("bottom", 15)
    sleep(5)
end
2018-10-14 08:52:16