我的代码一直循环。

我对 Lua 很不熟练,我的代码让我很困惑,我正在制作一个迷宫游戏来练手,但我发现了一个错误,每次运行代码都会进入循环,而不是进入下一步。感激任何提供帮助的人。

我的代码:

print ("欢迎来到迷宫")

input = ""
while input ~= "离开" do
    print ("你想先做什么?检查或离开?")
    input = io.read()

    if input == "检查" then
        print (" 你走向迷宫。")
    end

    if input == "离开" then
        print ("你转身就跑。")
    end
end

input = ""
while input ~= "转身" do
    print ("前方有路径,你想走哪条,左、右还是转身?")
    input = io.read()

    if input == "左" then
        print (" 你向左拐,进入黑暗的树林。")
    end

    if input == "右" then
        print ("你向右拐,进入光明的小路。")
    end

    if input == "转身" then
        print ("你转身就跑。")
    end
end
点赞
用户459750
用户459750

尽管这里的逻辑有些偏颇(一旦你 turn around 就会被要求再次 inspectleave),但以下就是如果你选择 inspect 迷宫时该如何到达第二部分:

print ("欢迎进入迷宫")

input = ""
while input ~= "leave" do
    print ("你想先做什么?离开还是探索?")
    input = io.read()

    if input == "inspect" then
        print ("你走向迷宫。")
        while input ~= "turn around" do
            print ("这里有一条路。你想选择向左走、向右走还是返回?")
            input = io.read()

            if input == "left" then
                print ("你向左转,进入黑暗的森林。")
            end

            if input == "right" then
                print ("你向右转,走上光明的小路。")
            end

            if input == "turn around" then
                print ("你转身就跑。")
            end
        end
    end

    if input == "leave" then
        print ("你转身就跑。")
    end
end
2016-01-29 13:36:12