Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 我的代码不断循环_Lua - Fatal编程技术网

Lua 我的代码不断循环

Lua 我的代码不断循环,lua,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 i

我是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

虽然这里的逻辑有点扭曲(一旦你
转身
你将被要求
检查
再次离开
),但下面是你如何进入第二部分-如果你选择
检查
迷宫,就需要这样做:

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

我不知道为什么代码会出错,但事实并非如此,对我来说,在SciTE上这很正常。你是从第一个while循环中出来的吗?从第一个while循环中,你会得到一个打印(“你想先做什么?离开还是检查?”)。如果你现在输入“离开”,他会走出这个循环,进入第二个循环,让你打印(“有一条路,你想走哪条路,左,右还是转弯?”)?我还没有越过那一点:我很困惑你是如何到达那一点的。。。请解释一下,逻辑思考一下。你遇到了一个迷宫,你有两个选择,检查,或离开。如果你检查,你就会进入迷宫。你不能离开迷宫,然后在迷宫中左转或右转。这就是您的第一个while循环所说的-键入
leave
以进入下一段代码。这里仍然存在一个逻辑问题,因为在你
转身的任何时候
,你都可以神奇地
在下一次输入时离开
迷宫,而不需要找到回到起点的路。因此,我是否应该划掉这个项目并做一些不同的事情呢?如果要帮助你学习,退出并不是真正的解决方案。试着弄清楚。如果需要的话,可以为解决方案提出一个新的设计。但是如果我的代码不符合逻辑,那么在将来我试图正确使用我的技能时,它将不会对我有帮助。