Lua中的这个“choose”函数总是导致nil

Lua中的这个“choose”函数总是导致nil,lua,Lua,从过去开始,这段代码确实起作用,然后函数突然将变量结果设为nil function choose(n1,n2) local r = math.random(1,2) if not n1 or not n2 then error("Choosing a number or a string results in nil.") end -- error if nil if r<1.5 then return n1 else

从过去开始,这段代码确实起作用,然后函数突然将变量结果设为nil

function choose(n1,n2)
    local r = math.random(1,2)
    if not n1 or not n2 then error("Choosing a number or a string results in nil.") end -- error if nil
    if r<1.5 then
        return n1
    else
        return n2
    end
end
function choose(v1,v2,v3)
    local r = math.random(1,3)
    if not v1 or not v2 or not v3 then error("Choosing a number or a string results in nil.") end --error if nil
    if r==1 then
        return v1
    elseif r==2 then
        return v2
    else
        return v3
    end
end

local Red,Yellow=2,12
choose(Red,Yellow)

Lua没有函数重载。它将假定您正在更新函数,并且总是调用最近定义的函数


在您的示例中,将引发错误,因为未定义v3。

Lua没有函数重载。它将假定您正在更新函数,并且总是调用最近定义的函数

在您的示例中,将引发错误,因为未定义v3

File: "C:\Users\My Computer\Desktop\test.lua" line 3,
    Choosing a number or a string results in nil.