我的卢阿有什么问题?

MrP*_*mbo 3 lua

我第一次涉足lua,这只是行不通.它说我试图调用全局exiter(零值).我只是在做一个简单的程序来尝试让函数工作.

print("hello world")
io.read()
y = 0

while y < 10 do
    local x = ""
    print("hello world")
    x = io.read()
    if x == "y" then
    y = exiter(1)
    print(y)
    end
end

function exiter(param)
    local q = 0
    print ("hello again")
    q = param * 10
    return q;
end
Run Code Online (Sandbox Code Playgroud)

hjp*_*r92 5

Lua程序从上到下逐个语句执行.因此,当您进入while循环时,该功能exiter尚未存在.在进入循环之前定义它:

function exiter(param)
    local q = 0
    print ("hello again")
    q = param * 10
    return q;
end

while y < 10 do
    local x = ""
    print("hello world")
    x = io.read()
    if x == "y" then
        y = exiter(1)
        print(y)
    end
end
Run Code Online (Sandbox Code Playgroud)