无法打印 "no"?

我对 Lua 很,很新,所以在学习它时我有一点卡住了。

为什么我不能在这里打印 "no"?我还应该添加什么?

if (expression_1) then
    if (expression_2) then
      print("yes")
    end
  else
  if (expression_3) then
      print("no")
    end
  end
点赞
用户2858170
用户2858170

如果想要打印出“no”,则 expression_1 不可为 truenilfalse),同时 expression_3 必须为 true

如果你的整个代码如下,是无法打印出“no”的,因为你还未为 expression_3 赋值,它的值为 nil

下面的代码将会打印出“no”:

local expression_3 = true
if (expression_1) then
  if (expression_2) then
    print("yes")
  end
else
  if (expression_3) then
    print("no")
  end
end

你也可以这样写:

local expression_3 = true
if expression_1 and expression_2 then
  print("yes")
elseif expression_3 then
  print("no")
end

顺便提一句,if 语句内的小括号是不必要的。

2017-08-01 16:11:21