Lua代码elseif不起作用

时间:2018-05-06 13:51:49

标签: lua lua-5.3

这就是接受来自用户的输入并使用该特定文本进行搜索。

使用string.gsub

io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D  ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:?  ")
accept=io.read()
if accept=="YES" or "yes" then
  io.write("\t WHICH TEXT TO REPLAE?  ")
  replace=io.read()
  --HERE IS THE REPLACING TEXT
  io.write("\t WITH WHAT:?   ")
  with=io.read()
  result=string.gsub(story,replace,with)
  print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or "no" then
  print(result)
end

错误:elseif循环不起作用!!

2 个答案:

答案 0 :(得分:1)

Customer==的工作方式类似于数学运算符,因为它们是一次评估一个,首先评估or。如果==accept'no'的评估方式如下:

accept=="YES" or "yes"

在Lua中,除(accept == "YES") or "yes" ('no' == "YES") or "yes" false or "yes" "yes" nil之外的所有值都是真实的,因此您的false块将始终运行,而不是if块。

如评论中所述,elseif将解决此问题。 accept:upper()=="YES"返回一个字符串,其中accept:upper()的所有字母都转换为大写字母,因此您只需将其与一个值进行比较。

答案 1 :(得分:0)

试试这个..

io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D  ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:?  ")
accept=io.read()
if accept=="YES" or accept == "yes" then
  io.write("\t WHICH TEXT TO REPLAE?  ")
  replace=io.read()
  --HERE IS THE REPLACING TEXT
  io.write("\t WITH WHAT:?   ")
  with=io.read()
  result=string.gsub(story,replace,with)
  print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or accept == "no" then
  print(result)
end