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
Lua 有没有办法进行更紧凑的答案检查_Lua - Fatal编程技术网

Lua 有没有办法进行更紧凑的答案检查

Lua 有没有办法进行更紧凑的答案检查,lua,Lua,我对编码非常陌生,现在我的代码非常庞大,我想知道是否有一种方法可以制作一个更紧凑的函数来检查答案,现在我只需要复制和粘贴if-then语句,每次都用不同的方式将变量大写和拼写,例如,不,我有N,N的if-then语句,不,不,不,不 local men = io.read() if men == "N" then print(" You decide that you're fine with getting pushed around for your

我对编码非常陌生,现在我的代码非常庞大,我想知道是否有一种方法可以制作一个更紧凑的函数来检查答案,现在我只需要复制和粘贴if-then语句,每次都用不同的方式将变量大写和拼写,例如,不,我有N,N的if-then语句,不,不,不,不

local men = io.read()
if men == "N" then 
    print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
    return
end
if men == "NO" then 
    print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
    return
end
if men == "no" then 
    print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
    return
end
if men == "No" then 
    print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
    return
end
if men == "n" then 
    print(" You decide that you're fine with getting pushed around for your whole life, so you continue like that until you are old and die. THE END")
    return
end

您可以将选项放入列表中,然后通过for循环询问您的输入是否等于列表中的语句。如果没有,就没有错

例如,如果您有如下列表:

l_words = ["N","n","no","NO","N0","nO"]

inp = input("Your input: ")

for i in l_words:
    if inp != i:
        print("true input")
    else:
        print("false input")
你可以用。它们与大多数编程语言中使用的正则表达式非常相似。在这里,我测试答案字符串,看看它是否匹配您正在寻找的模式。下面是对模式的解释:

  • ^
    -匹配字符串的开头,在此之前不允许有任何字符。如果不包含此项,则在字符串后面可能会找到“否”,即
    abcdNO
  • [nN]
    -
    []
    允许您包括可接受字符的列表,因此这里的第一个字符必须是
    n
    n
  • [oO]?
    -下一个字符必须是
    o
    o
    ,但
    表示它是可选的,可以出现0或1次
  • $
    匹配字符串的结尾,因此它将不匹配'NOabcd',因为在您的模式之后不能有任何内容
总之,这意味着字符串必须以'n'或'n'开头,可能只有一个'o'或'o',之后就没有别的了

string.find(string,pattern)
将查看字符串是否与pattern匹配,并返回在字符串中找到的位置,如果未找到,则返回
nil

local answer='No'
本地模式='^[nN][oO]?$'
如果(string.find(answer,pattern)~=nil),那么
打印('找到!')
其他的
打印('未找到!')
结束
您可以使用,然后检查输入是否为集合的成员。Lua中的简单集合可以这样定义:

本地编号={
N=正确,
n=正确,
否=正确,
否=正确,
否=正确,
否=真
}
您只需像任何表一样对其进行索引即可使用它:

localmen=io.read()
如果没有人那么
打印(“你决定你可以一辈子被人摆布,所以你会一直这样,直到你老了,死了。结束”)
返回
结束

lua中的
nil
在本文中将被视为
false
,您将从集合中任何非键的值中获得
nil
,我正在添加另一个答案以提供更简单的更改。您通常应该设法找到避免重复代码的方法。相同的“打印”语句重复多次

可以做的一件事是使用并将所有测试组合到一个表达式中

localmen=io.read()
如果男性==“N”或男性==“NO”或男性==“NO”或男性==“NO”或男性==“N”,则
打印(“你决定你可以一辈子被人摆布,所以你会一直这样,直到你老了,死了。结束”)
返回
结束
这是进行设置的最佳方法,但是如果您没有这样一个简单的条件,并且希望重用代码,那么您可以使用重复的代码创建一个函数并调用它。如果您还想根据某些值做其他事情:


函数badEnding()
打印(“你决定你可以一辈子被人摆布,所以你会一直这样,直到你老了,死了。结束”)
--注意:不需要返回,默认返回值为零
结束
当地人=io.read()
如果men==“N”,则返回badEnding()结束
如果男人==“不”,那么
打印(“嘿,不用喊了!”)
返回坏帐()
结束
如果men==“no”,则返回badEnding()结束
如果men==“No”,则返回badEnding()结束
如果men==“n”,则返回badEnding()结束

您可以编辑您的问题以包含您的代码吗?这不是
Lua
,此问题用
Lua
编程语言标记。