io.write每次打印相同的东西?

时间:2013-10-04 12:00:44

标签: io lua

我对lua和编程非常陌生,目前我正在制作一个基本程序来熟悉if / else / elseif和i / o。

基本上,它会询问你是男孩还是女孩,然后相应打印出来。

虽然,我有一个问题,每次我运行程序时,它总会打印出你是男孩。

对不起,如果我是白痴,我们非常感谢您的帮助!

代码如下;

io.write("Hello, are you a boy or a girl?\n")
answer = io.read()
if answer == "boy" or "Boy" then
io.write("Oh, so you're a boy, huh?")
elseif answer == "girl" or "Girl" then
io.write("Oh, so you're a girl, huh?")
else
io.write("Wow, i've never heard of an "..answer.." before!\n")
end

2 个答案:

答案 0 :(得分:2)

尝试

io.write("Hello, are you a boy or a girl?\n")

answer = io.read()

if answer == "boy" or answer == "Boy" then
  io.write("Oh, so you're a boy, huh?")
elseif answer == "girl" or answer == "Girl" then
  io.write("Oh, so you're a girl, huh?")
else
  io.write("Wow, i've never heard of an "..answer.." before!\n")
end

解释

Docs说:

  

如果此值与nil和false不同,则析取运算符或返回其第一个参数;否则,或返回其第二个参数。

如果回答==“女孩”的步骤:

  1. answer == "boy" or "Boy"
  2. false or "Boy"
  3. "Boy"
  4. 字符串被视为true - >执行if

答案 1 :(得分:2)

尝试替换它:

if answer == "boy" or "Boy" then

有了这个:

if answer == "boy" or answer == "Boy" then

与其他案件类似。