如何在Lua中建立密码系统?

时间:2016-11-28 16:28:39

标签: lua password-protection

我正在尝试建立一个密码系统,但出于某种原因,我希望它在密码正确时运行的代码不会运行。我不确定是什么问题,但我正在寻找问题的解决方案,或者简单地建立密码系统的另一种方法。 这就是我所拥有的:

term.setTextColor( colors.red )
print("Password Required")
term.setTextColor( colors.cyan )
io.write("Password:")
local password = io.read()

while (password ~= "Password") do

    io.write("Incorrect")
    print("")
    io.write("Password:")
    password = io.read()
    if password=="Password" then
        sleep(.5)
        term.setTextColor( colors.green )
        print("Access Granted")
    end
end

1 个答案:

答案 0 :(得分:1)

使用while循环可以实现此问题的基本解决方案。

term.setTextColor( colors.red )
print("Password Reqired")

while true do
    term.setTextColor(colors.cyan)
    io.write("Password:\t")
    term.setTextColor(colors.white)
    password = io.read()
    if password=="Password" then
        sleep(.5)
        term.setTextColor(colors.green)
        print("Access Granted")
        break
    else
        term.setTextColor(colors.red)
        print("Access Denied")
    end
end

这会不断重复,直到提供正确回答的密码,其中break会逃脱循环。

但是,根据您对“期限”的要求判断,我假设您正在使用ComputerCraft。如果是这样,您可能还希望阻止终止,这可以通过覆盖错误功能来完成。

term.setTextColor( colors.red )
print("Password Reqired")


function error(txt)
    term.setTextColor(colors.red)
    print(txt)
end

while true do
    term.setTextColor(colors.cyan)
    io.write("Password:\t")
    term.setTextColor(colors.white)
    password = io.read()
    if password=="Password" then
        sleep(.5)
        term.setTextColor(colors.green)
        print("Access Granted")
        break
    else
        term.setTextColor(colors.red)
        print("Access Denied")
    end
end

但请注意,如果有人可以将磁盘驱动器放在您的计算机旁边,他们仍然可以进入。

相关问题