lua - if 语句在 Lua 中不适用于 io.read

标签 lua

我正在尝试做一个“简单”的是/否答案选择。 (您在旧程序中一直看到这一点)但是我正在使用的 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

所做的更改是:

  1. 在 while 循环内部获取用户输入,这样,如果输入无效并且循环再次使用获取输入背后的相同逻辑,我们不会不必编写两种情况来获取输入;一个在循环外,另一个在循环内。当循环再次开始时,它还会暂停执行,这就是产生所有输出的原因!
  2. input == "y" or "Y"不按你的想法做。相反,它的计算结果为 (input == "y") or ("Y") ,你想要什么吧input == "y" or input == "Y" .
  3. 您需要将循环设置为 false当输入为"y" or "Y" or "n" or "N"时,否则循环将继续。
  4. 第四次将循环设置为 true循环内部是不必要的,它以 true 开头,您可以进行的唯一更改是将其设置为 false 。并且由于每个条件都是互斥的,即输入为 "y" or "Y"与输入互斥 "n" or "N"或者两者都不是 "y" or "Y""n" or "N" 。除非您希望循环结束,否则您无需担心它被设置为 false。

关于lua - if 语句在 Lua 中不适用于 io.read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621814/

相关文章:

parsing - 使用 lpeg 仅捕获字边界

sockets - lua http.request 返回尝试索引全局 'http'(一个 nil 值)

c++ - Lua 函数什么都不做

c - LuaJIT FFI : Uploading Steamworks leaderboards

string - Lua读取字符串的开头

math - LUA中的正态分布曲线和随机数

node.js - Aerospike NodeJS UDF 聚合错误

unicode - 我如何使用 io.open 在 lua 中打开 unicode 路径

PHP Lua注册系统

Lua Pattern 匹配一个模式