首页 > 开发 > 综合 > 正文

Lua脚本语言基本语法快速入门教程

2024-07-21 23:04:58
字体:
来源:转载
供稿:网友

Lua语法与C语言有些相似也不太一样,写了语句学一下 微笑

--begina = --[[explain]] "ha";print(a)if a == "ha" then print("if test passed")else print("if used wrong")endb,c=2,3print(b,c)b,c=c,bif b==3 and c==2 then print("swap test passed") print(b,c)else print("swap error")enddo b = 6 if b==6 then print("code block test passed") else print("code block test error") endenddo d=true; local e="haha"endif d==true and e==nil then print("local test passed")else print("local test error")endc = 2^3if c == 8 then print("squert test passed")else print("test error")enda = "string will be ".."connected"print(a)x = x or a --if not x then x = v endprint(x)print(type(asdf))c=3-1.2;print(c)d = [[怎么会 怎么会 你竟原谅了我?]]print(d)function test (w) print("the num is "..w) local add=w+1 return add;endb=test(5)print(add,b)t={ 100, [100] = "I'm the 100th element", fsy= { ['age']=22, sex = "male", --如果是字符串,可以去掉引号和括号 },--元素之间必须用,隔开 20, -- 相当于t[2]=20}print(t[0])print(t[1])print(t[100])print(t.fsy.age)print(t[2])g = { age = 3, add=function (s,n) s.age=s.age+n end}g:add(10) --相当于g.add(g,10)print(g.age)

运行的结果如下:

>lua -e "io.stdout:setvbuf 'no'" "hello.lua" haif test passed2 3swap test passed3 2code block test passedlocal test passedsquert test passedstring will be connectedstring will be connectednil1.8怎么会 怎么会 你竟原谅了我?the num is 5nil 6nil100I'm the 100th element222013>Exit code: 0
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表