4个参数的功能,可以少于4个

Ren*_*gue 1 lua

所以我必须编写一个函数join(a,b,c,d),它最多需要4个字符串,并用逗号连接起来.当给定少于4个字符串时,该函数仍然可以工作,包括只给出一个没有逗号的字符串,只给出一个字符串作为参数.但是,我只能使用它3

function join(a,b,c,d)
    if b == nil then
      print(a .. ", " .. c .. ", " .. d) 
    elseif c == nil then
        print(a .. ", " .. b .. ", " .. d) 
    elseif d == nil then
        print(a .. ", " .. b .. ", " .. c) 
    else
        print(a .. ", ".. b .. ", " .. c .. ", " .. d) 
    end
end
Run Code Online (Sandbox Code Playgroud)

我不知道如何使它少于3个参数,请帮忙

Vla*_*lad 5

如果只连接并打印这些字符串输入,则甚至不需要为参数指定名称:

local function join(...)
    print(table.concat({...}, ", ")
end

join("a")
join("a","b")
join("a","b","c")
join("a","b","c","d","e","f","blah-blah","as many as you want", "even more")
Run Code Online (Sandbox Code Playgroud)