Lua的五種變量類型、局部變量、全局變量 、lua運算符 、流程控制if語句
-
Lua代碼的注釋方式:
--當行注釋
--[[ 多行注釋 ]]--
-
Lua的5種變量類型:
1.null 表示空數據 等同於null
2.boolean 布爾類型 存儲true 和false
3.String 字符串類型,字符串可以用雙引號也可以用單引號表示
4.number小數類型(lua中沒有整數類型?
5.table類型
myTable = {34.31.30}
myTable[3] 注意 Lua中的索引是從1開始的。
可以用type()來取得一個變量的類型
-
全局變量和局部變量
默認定義的變量都為全局變量,定義局部變量需要在前面加一個local 。
在代碼塊中聲明的為局部變量,當代碼塊運行結束的時候,這個變量則會被釋放
-
lua中運算符
1.算術運算符+-*/%(lua中沒++ -- 這樣的運算符)
2.關系運算符 <= < > >= ==
3.邏輯運算符 and or not 分別表示與 或 非 (類似於C#中的 && || !)
-- and 運算符的使用 (下面為特殊的用法)
-- 如果第一個表達式為 true ,而 第二個表達式的運算結果是一個非布爾的值,則輸出這個值
print(26<27 and 3) --3
print(true and 4) --4
-- or 運算符的使用 (下面為特殊的用法)
--如果第一個表達式為false ,而第二個表達式的運算結果是一個非布爾型的值,則輸出這個值
print(1>2 or 5) --5
-- 非 not 在lua中所有不是 false 和 nil 的值都代表 true
-- 連接 .. (用於連接兩個字符串 ) eg: print(“521”.."1234") -- 5211234
-
Lua的流程控制if語句
1.if 表達式 then
語句塊
end
2.if 表達式 then
語句塊
else
語句塊
end
3.if 表達式 then
語句塊
elseif 表達式 then
語句塊
else
語句塊
end
下面是 if 例子
-- 一個數 90-100 優秀 70-90 良好 60-70 及格 小於60大於100 不及格 local mathaa = 80 if 90 <=mathaa and mathaa<=100 then print ("優秀") elseif 70 <= mathaa and mathaa <=90 then print("良好") elseif 60<=mathaa and mathaa<=70 then print("及格") else print("不及格") end --********************************************************************************************* --寫一個腳本 判斷一個數是否為大於100的偶數 local shu = 260 if shu % 2 == 0 and shu >100 then print("此數為一個偶數") else print("此數不是一個偶數") end --********************************************************************************************** --寫一個腳本判斷兩個數是否 都為 小於或等於 200的奇數 (不能被2整除的數) local a ,b = 110,330 if a%2==1 and a<=200 and b<=200 and b%2==1 then print ("ab 兩個數都為小於等於200的奇數") elseif a%2==1 and a<=200 then print ("a為小於等於200的奇數") elseif b<=200 and b%2==1 then print ("B為小於等於200的奇數") else print ("ab 兩個數都不是小於等於200的奇數") end