首先是lua的注釋
1 -- 單行注釋
2
3 --[[
4 多行注釋
5 --]]
2
3 --[[
4 多行注釋
5 --]]
lua有8種類型:nil, boolean, number, string, userdate, function, thread, table
nil 就是 null
boolean 有 true 和 false 兩種, 除了 false 和 0 以外, 其他情況都是 true
number 代表所有數值類型,包括浮點型
string 表示string類型有3種方法
1 "雙引號字符串"
2
3 '單引號字符串'
4
5 [[
6 多行字符串
7 ]]
2
3 '單引號字符串'
4
5 [[
6 多行字符串
7 ]]
連接字符串用".."
1 print("Hello".."World") -- 輸出結果:HelloWorld
table
Table
賦值
1 -- 單值
2 a = "Hello".."World" -- 輸出結果:HelloWorld
3
4 -- 多值
5 a, b = 10, 20
6 print a -- 輸出結果:10
7 print b -- 輸出結果:20
2 a = "Hello".."World" -- 輸出結果:HelloWorld
3
4 -- 多值
5 a, b = 10, 20
6 print a -- 輸出結果:10
7 print b -- 輸出結果:20
聲明局部變量
1 local i = 0
if語句
1 local i = 0
2
3 if i == 0 then
4 print("red")
5 elseif i == 1 then
6 print("white")
7 else
8 print("black")
9 end
2
3 if i == 0 then
4 print("red")
5 elseif i == 1 then
6 print("white")
7 else
8 print("black")
9 end
while 語句
1 local i = 0
2
3 while true do
4 print("~~~~~")
5
6 if i == 1 then
7 print("red")
8 break
9 end
10
11 i = i + 1
12 end
2
3 while true do
4 print("~~~~~")
5
6 if i == 1 then
7 print("red")
8 break
9 end
10
11 i = i + 1
12 end
repeat utile 語句
1 local i = 0
2
3 repeat
4 print("~~~~~")
5
6 i = i + 1
7 until i > 3
2
3 repeat
4 print("~~~~~")
5
6 i = i + 1
7 until i > 3
for 語句
1 -- 普通 for 循環
2 for i = 1, 10 do
3 print(i)
4 end
5
6 -- 泛型 for 循環
7 color = {"red", "white", "black"}
8
9 for i, v in ipairs(color) do
10 print("~~~start~~~")
11 print(i)
12 print(v)
13 print("~~~ end ~~~")
14 end
2 for i = 1, 10 do
3 print(i)
4 end
5
6 -- 泛型 for 循環
7 color = {"red", "white", "black"}
8
9 for i, v in ipairs(color) do
10 print("~~~start~~~")
11 print(i)
12 print(v)
13 print("~~~ end ~~~")
14 end