我的代码一直在循环

时间:2016-01-29 13:24:35

标签: lua

我对Lua很新,而且我的代码让我感到困惑,我正在制作一个迷宫游戏,只是为了练习而且我遇到了一个错误,每次我运行我的代码循环,而不是去下一部分。我将不胜感激任何帮助。

我的代码:

print ("Welcome to the maze")

input = ""
while input ~= "leave" do
    print ("What do you want to do first? Leave or inspect?")
    input = io.read()

    if input == "inspect" then
        print (" You venture towards the maze.")
    end

    if input == "leave" then
        print ("You turn around and run.")
    end
end

input = ""
while input ~= "turn around" do
    print ("There is a path, which do you want to take, left, right or turn around?")
    input = io.read()

    if input == "left" then
        print (" You turn left to the dark trees.")
    end

    if input == "right" then
        print ("You turn right to the light pathway.")
    end

    if input == "turn around" then
        print ("You turn around and run.")
    end
end

1 个答案:

答案 0 :(得分:1)

虽然此处的逻辑略有偏差(一旦turn around,您将再次被要求inspectleave,这就是您将如何进入第二部分 - 它需要如果您选择inspect迷宫,则会发生:

print ("Welcome to the maze")

input = ""
while input ~= "leave" do
    print ("What do you want to do first? Leave or inspect?")
    input = io.read()

    if input == "inspect" then
        print (" You venture towards the maze.")
        while input ~= "turn around" do
            print ("There is a path, which do you want to take, left, right or turn around?")
            input = io.read()

            if input == "left" then
                print (" You turn left to the dark trees.")
            end

            if input == "right" then
                print ("You turn right to the light pathway.")
            end

            if input == "turn around" then
                print ("You turn around and run.")
            end
        end
    end

    if input == "leave" then
        print ("You turn around and run.")
    end
end