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
if语句在Lua for io.read中不起作用_Lua - Fatal编程技术网

if语句在Lua for io.read中不起作用

if语句在Lua for io.read中不起作用,lua,Lua,我正在尝试做一个简单的答案选择。(您一直在旧程序上看到)但是我使用的If语句似乎不起作用。我甚至打印出了变量,它与我想要比较的东西相差甚远,但它仍然传递它 --Porgram Functions function check() --Local Variables local num = 0 local loop = true io.write("Continue? (Y/

我正在尝试做一个简单的答案选择。(您一直在旧程序上看到)但是我使用的If语句似乎不起作用。我甚至打印出了变量,它与我想要比较的东西相差甚远,但它仍然传递它

        --Porgram Functions

        function check()

        --Local Variables
            local num = 0
            local loop = true
            io.write("Continue? (Y/N):")
            --User input
            local input = io.read()

            while(loop==true) do

                if (input=="y" or "Y") then

                    print("Ok!")
                    loop = true
                    num = 1

                elseif (input=="n" or "N") then

                    print("Fine...")
                    num = 2

                else

                    print("Invalid Answser!")
                    loop = true
                    num = 0

                end
            end
            print(input)
            return(num)
        end

        print (check())

我会这样写你的函数:

function check()
    io.write("Continue? (Y/N): ")
    answer = io.read()
    while( not (answer == "Y" or answer == "N") ) do
        io.write("Invalid Answer! Try again (Y/N): ")
        answer = io.read()
    end
    if answer == "Y" then
        print("Ok!")
        return 1
    else
        print("Fine...")
        return 2
    end
end

print(check())
其使用的一些示例:

Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): N
Fine...
2
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "a.lua" 
Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): Y
Ok!
1

您的代码的工作版本为:

function check()
    local num = 0
    local loop = true    
    io.write("Continue? (Y/N):")

    while(loop==true) do    
        --User input
        local input = io.read()   
        if (input == "y" or  input == "Y") then    
            print("Ok!")
            num = 1
            loop = false --we want to stop looping if input is valid      
        elseif (input == "n" or input == "N") then    
            print("Fine...")
            num = 2
            loop = false --we want to stop looping if input is valid   
        else
            print("Invalid Answser!")
            -- loop = true no need to set looping to true again
            num = 0    
        end
    end
    return(num)
end
所作的修改是:

  • 在while循环中获取用户输入,这样,如果输入无效,循环再次使用获取输入的相同逻辑,我们不必为获取输入编写两种代码;一个在环路外,另一个在环路内。当循环再次启动时,它还会暂停执行,这就是产生所有输出的原因
  • input==“y”或“y”
    不会按您的想法操作。相反,它的计算结果为
    (input==“y”)或(“y”)
    ,即您想要的
    input==“y”或input==“y”
  • 当输入为“y”或“y”或“n”或“n”时,需要将循环设置为
    false
    ,否则循环将继续
  • 第四,在循环内部将循环设置为
    true
    是不必要的,它以
    true
    开始,您可以做的唯一更改是将其设置为
    false
    。并且,由于每个条件都是互斥的,即输入为
    “y”或“y”
    ,因此输入为
    “n”或“n”
    ,或者输入既不是
    “y”或“y”或
    “n”或“n”
    。您不必担心它被设置为false,除非您希望循环结束

  • 我会这样写你的函数:

    function check()
        io.write("Continue? (Y/N): ")
        answer = io.read()
        while( not (answer == "Y" or answer == "N") ) do
            io.write("Invalid Answer! Try again (Y/N): ")
            answer = io.read()
        end
        if answer == "Y" then
            print("Ok!")
            return 1
        else
            print("Fine...")
            return 2
        end
    end
    
    print(check())
    
    其使用的一些示例:

    Continue? (Y/N): Huh?
    Invalid Answer! Try again (Y/N): N
    Fine...
    2
    >Exit code: 0
    >lua -e "io.stdout:setvbuf 'no'" "a.lua" 
    Continue? (Y/N): Huh?
    Invalid Answer! Try again (Y/N): Y
    Ok!
    1
    

    您的代码的工作版本为:

    function check()
        local num = 0
        local loop = true    
        io.write("Continue? (Y/N):")
    
        while(loop==true) do    
            --User input
            local input = io.read()   
            if (input == "y" or  input == "Y") then    
                print("Ok!")
                num = 1
                loop = false --we want to stop looping if input is valid      
            elseif (input == "n" or input == "N") then    
                print("Fine...")
                num = 2
                loop = false --we want to stop looping if input is valid   
            else
                print("Invalid Answser!")
                -- loop = true no need to set looping to true again
                num = 0    
            end
        end
        return(num)
    end
    
    所作的修改是:

  • 在while循环中获取用户输入,这样,如果输入无效,循环再次使用获取输入的相同逻辑,我们不必为获取输入编写两种代码;一个在环路外,另一个在环路内。当循环再次启动时,它还会暂停执行,这就是产生所有输出的原因
  • input==“y”或“y”
    不会按您的想法操作。相反,它的计算结果为
    (input==“y”)或(“y”)
    ,即您想要的
    input==“y”或input==“y”
  • 当输入为“y”或“y”或“n”或“n”时,需要将循环设置为
    false
    ,否则循环将继续
  • 第四,在循环内部将循环设置为
    true
    是不必要的,它以
    true
    开始,您可以做的唯一更改是将其设置为
    false
    。并且,由于每个条件都是互斥的,即输入为
    “y”或“y”
    ,因此输入为
    “n”或“n”
    ,或者输入既不是
    “y”或“y”或
    “n”或“n”
    。您不必担心它被设置为false,除非您希望循环结束
  • 本地功能检查()
    io.写下“继续?(是/否):”
    本地ans,num={y=1,n=2}
    重复
    num=ans[io.read():lower()]或3
    io.write(({“确定!\n”,“良好…”\n”,“回答无效!重试(Y/n):“})[num])
    直到num<3
    返回数
    结束
    打印(检查()
    
    本地功能检查()
    io.写下“继续?(是/否):”
    本地ans,num={y=1,n=2}
    重复
    num=ans[io.read():lower()]或3
    io.write(({“确定!\n”,“良好…”\n”,“回答无效!重试(Y/n):“})[num])
    直到num<3
    返回数
    结束
    打印(检查()
    
    另请参见。另请参见。@lhf确实如此,但我觉得我的版本更可读,但这只是我的偏好。如果您指的是我对OP的代码所做的修改,我会尽量使其与它们的实现保持相似。谢谢!重复输出更多的是我忘记删除的调试功能。然而,这帮了大忙!我觉得没有把输入放在循环中有点愚蠢…@lhf这是真的,但我觉得我的版本更可读,但这只是我的偏好。如果您指的是我对OP的代码所做的修改,我会尽量使其与它们的实现保持相似。谢谢!重复输出更多的是我忘记删除的调试功能。然而,这帮了大忙!我觉得有点愚蠢,因为我没有把输入放在循环中。。。