Lua語法與C語言有些相似也不太一樣,寫了語句學(xué)一下 微笑
--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 = [[怎么會(huì) 怎么會(huì) 你竟原諒了我?]]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", --如果是字符串,可以去掉引號(hào)和括號(hào) },--元素之間必須用,隔開 20, -- 相當(dāng)于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) --相當(dāng)于g.add(g,10)print(g.age)
運(yùn)行的結(jié)果如下:
>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怎么會(huì) 怎么會(huì) 你竟原諒了我?the num is 5nil 6nil100I'm the 100th element222013>Exit code: 0
|
新聞熱點(diǎn)
疑難解答
圖片精選