服务器之家

服务器之家 > 正文

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

时间:2020-05-05 13:21     来源/作者:脚本之家

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
--begin
a = --[[explain]] "ha";
print(a)
 
if a == "ha" then
 print("if test passed")
else
 print("if used wrong")
end
 
b,c=2,3
print(b,c)
b,c=c,b
if b==3 and c==2 then
 print("swap test passed")
 print(b,c)
else
 print("swap error")
end
 
do
 b = 6
 if b==6 then
 print("code block test passed")
 else
 print("code block test error")
 end
end
 
do
 d=true;
 local e="haha"
end
if d==true and e==nil then
 print("local test passed")
else
 print("local test error")
end
 
c = 2^3
if c == 8 then
 print("squert test passed")
else
 print("test error")
end
 
a = "string will be ".."connected"
print(a)
 
x = x or a --if not x then x = v end
print(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;
end
 
b=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)

运行的结果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>lua -e "io.stdout:setvbuf 'no'" "hello.lua"
ha
if test passed
2 3
swap test passed
3 2
code block test passed
local test passed
squert test passed
string will be connected
string will be connected
nil
1.8
怎么会
 怎么会
 你竟原谅了我?
 
 
the num is 5
nil 6
nil
100
I'm the 100th element
22
20
13
>Exit code: 0

相关文章

热门资讯

沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部