local n = -42
-- Lua 没有三元运算符:用 cond and a or b 代替
local classify =
n > 0 and "positive" or "negative"
-- classify = negative
-- 陷阱:a 为 false 或 nil 时,仍会选中 b
local answer = false
local wrong = n < 0 and answer or "other"
-- wrong = other
-- a 可能为 false 或 nil 时,改用 if
local right
if n < 0 then
right = answer
else
right = "other"
end
-- right = false
print("classify = " .. classify)
print("wrong = " .. wrong)
print("right = " .. tostring(right))