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,这是我的密码: repeat ... print("would you like to do that again?") answer = io.read() until answer == "no" or answer == "n" or answer == "nope" 显然,使用“nope”是陈词滥调,但关键是:如何避免使用或使用过多?有没有办法将所有可能的比较值放在一个列表中,并将所述列表与给定字符串进行比较?您可以使用函数来处理列表,例如 function inLis

这是我的密码:

repeat

  ...

  print("would you like to do that again?")
  answer = io.read()
until answer == "no" or answer == "n" or answer == "nope"

显然,使用“nope”是陈词滥调,但关键是:如何避免使用或使用过多?有没有办法将所有可能的比较值放在一个列表中,并将所述列表与给定字符串进行比较?

您可以使用函数来处理列表,例如

function inList(value,list)
  value  = value:lower()
  for k,v in ipairs(list) do
    if v == value then
            return true
    end
  end
  return false
end

print(inList('yes',{'no','nope','n'}))

if inList('No',{'no','nope','n'}) then
    print('Is in List')
end

它比您已有的简单或语句更需要处理器,但是如果您需要处理大量变化,它可能会更容易。我包含了:lower命令,因此No、NOPE等也将返回true。

您可以使用函数来处理列表,例如

function inList(value,list)
  value  = value:lower()
  for k,v in ipairs(list) do
    if v == value then
            return true
    end
  end
  return false
end

print(inList('yes',{'no','nope','n'}))

if inList('No',{'no','nope','n'}) then
    print('Is in List')
end

它比您已有的简单或语句更需要处理器,但是如果您需要处理大量变化,它可能会更容易。我包含了:lower命令,因此No、NOPE等也将返回true。

正如lhf所说,您提供的代码非常简单。但是,如果要对多组单词或更大的单词集执行此操作,则可能需要使用Lua表作为集合。下面是一个例子:

is_no = { ["no"]= true; ["n"]= true; ["nope"]= true; }


repeat
  -- ...
  print("would you like to do that again?")
  answer = io.read()
until is_no[answer]

正如lhf所说,您提供的代码非常简单。但是,如果要对多组单词或更大的单词集执行此操作,则可能需要使用Lua表作为集合。下面是一个例子:

is_no = { ["no"]= true; ["n"]= true; ["nope"]= true; }


repeat
  -- ...
  print("would you like to do that again?")
  answer = io.read()
until is_no[answer]

您的代码简单高效。不需要把它复杂化。你可以做否定逻辑(直到回答!=“是”)-这样,一切都是“不”,除非它是“是”。@Lawrence,但我想他会希望“是”和“是”作为“是”的变体…@Ihf,这取决于方法。但你很可能是对的,你的代码简单高效。不需要把它复杂化。你可以做否定逻辑(直到回答!=“是”)-这样,一切都是“不”,除非它是“是”。@Lawrence,但我想他会希望“是”和“是”作为“是”的变体…@Ihf,这取决于方法。但你很可能是对的。