首先给个swap函数
i1,i2=3,4 print(i1,i2) i1,i2=i2,i1 print(i1,i2)
--单行注释 (大小写敏感) --[[ 保留符 and break do else elseif end false for function if in local nil not or true repeat return then until while ]]--
days = {"Suanday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" } for i,v in ipairs(days) do print(v) end function f(x) print("function") return x*2 end for i=1,f(5) do print(i) end
Lua while 语句使用
i = 1 while i <= 5 do print("i is now " .. i) if i < 2 then print("small") elseif i < 4 then print("medium") else print("big") end i = i + 1 end
Lua repeat语句使用
i = 1 repeat print("i is now " .. i) if i < 2 then print("small") elseif i < 4 then print("medium") else print("big") end i = i + 1 until i > 5Lua if语句使用
do i3=10 if i3==10 then print "pass" else print "no pass" end end
print(type(print)) print(type("hello world ")) print(type(true)) print(type(12)) print(type(nil))Lua 函数举例:
阶乘函数实现:
function JC(n) if n==0 then return 1 else return n*JC(n-1) end end print("Please Enter a number:") a=io.read("*number") print(JC(a))求均方函数:
function norm(x,y) local n=x^2+y^2 return math.sqrt(n) end print(norm(12,4))
原文:http://blog.csdn.net/sunboyiris/article/details/23761765