您的位置:首页 > 编程语言 > Lua

lua基础语法

2016-03-24 18:05 1236 查看
简单的语法,自己留个学习笔记而已

if(0)
then
print("0 is true")
end

a = 1

if(a)
then
print("1 is false")
else
print("a is true")
end

a = 100
if(a == 50)
then
print("the value of a is 50")
elseif(a == 100)
then
print("the value of a is 100")
else
print("the value of a is not matched")
end

-- test loop of 'for'

days = {'sunday','monday','tuesday','wednesday','thursday','friday','saturday'}
for i,v in ipairs(days) do print(i,v) end

-- test loop of 'repeat'
a = 10
repeat
print('the value of a is ',a)
a = a + 1
until(a > 15)

-- test for function in lua
myprint = function(param)
print("this is print fun ###",param,"###")
end

function add (num1,num2,functionprint)
result = num1 + num2
-- call function from passed
functionprint(result)
end

myprint(100)

add(10,20,myprint)

-- test for the number of args is not sure
function average(...)
result = 0
local arg = {...}
for i,v in ipairs(arg)
do
result = result + v
end
print('the number of arg is'..#arg..'size')
return result/#arg
end

print("the average is ",average(10,5,6,20,2))


运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: