E

@Ellen_Lin

Test_Module_mymath

Lua
3 years ago
local MM = require("mymath") MM.add(10,2) MM.sub(10,2) MM.mul(10,2) MM.div(10,2)

Module_mymath

Lua
3 years ago
local mymath = {} function mymath.add(a,b) print(a+b) end function mymath.sub(a,b) print(a-b) end function mymath.mul(a,b)

EX_6

Lua
4 years ago
-- 6.宣告一個function,送出以下http request,檢查回傳結果是否正確(包含 "0_SFC_OK"的字串), -- 若回傳正確,請將DUT的資訊解析出來存成table,並將table內容以Key-Value的方式印出, -- 若回傳不正確就報錯 -- (連公司內網) -- "http://172.26.40.140/Bobcat-V2101/sfc_response.aspx?c=QUERY_RECORD&p=front_nvm_barcode,CPU_OPTION,STORAGE_OPTION,battery_sn,mpn,region_code,RAM_OPTION,mesa_sn,trackpad_sn,keyboard_sn&sn=DL69H3HTCJ"

EX_4

Lua
4 years ago
-- 4.宣告一個function,輸入任意一個文字檔案的路徑,將內容打印出來,若檔案路徑找不到檔案要報錯 function readFile(fileName) local file = io.open(fileName, "r") local content = file:read("*all") --*all表示讀取所有內容,*line表示讀取一行內容,*number表示讀取一個數字,如果第一個字元為數字則返回這個數字,否則返回nil file:close() return content end

EX_5

Lua
4 years ago
-- 5.宣告一個function,輸入一個正整數,顯示所有小於等於該正整數的質數,若輸入的數非正整數要報錯 -- ex: -- input : 5 -- return : 1,2,3,5 -- input : 10 -- return : 1,2,3,5,7 -- input : "a" -- return : "Error, input must be a positive integer" function Prime_number(x)

EX_3

Lua
4 years ago
-- 3.宣告一個function,輸入一段字串,將該字串用空白字符分割,結果裝到陣列回傳,若輸入非字串要報錯 -- ex: -- input : "This is a sentence" -- return : {[1] : "This", [2] : "is", [3] : "a", [4] : "sentence"} -- input : 100 -- return : "Error, input must be a string" function EX_3(x) if(type(x) == "string") then local array={}

Notes

Lua
4 years ago
-- boolean 類型只有兩個 true / false -- Lua 把 false / nil 當作 false -- 其他都為 true, 0 也是 true if false or nil then print("至少有一个是 true") else print("false 和 nil 都為 false") end

EX_2

Lua
4 years ago
-- 2.宣告一個function,輸入一個正整數X,回傳一個高度為 X 的金字塔,若輸入非正整數要報錯 -- ex: -- input : 3 -- return : -- * -- * * -- * * * -- input : "a" -- return : "Error, input value must be a positive integer"

EX_1

Lua
4 years ago
-- 1.宣告一個function,輸入一個正整數X,回傳X階層(X!),若輸入非正整數要報錯 -- ex: -- input : 5 -- return : 120 -- input : 0 -- return : "Error, input value must be a positive integer" function EX_1(x) if(type(x) == "number" and x > 0 and x%1==0)

Lua- Modules

Lua
4 years ago
---[[ Modules ]]--- -- Include code other files -- 可以加載需要使用的庫 ---[[ Method 1 ]]--- -- require("myMath") -- myMath.function(a,b) ---[[ Method 2 ]]---

Lua- Scope and Loops

Lua
4 years ago
--- Scope --- j = 10 -- global variable local i = 1 -- local variable -- Unlike global variables, local variables have their scope limited to the block where they are declared. -- A block is the body of a control structure, the body of a function, or a chunk. print("----Local Variables and Blocks----") x = 10 -- global variable

Lua- Functions

Lua
4 years ago
--- Functions --- print("----連加----") function test(s, e) local a = 0 for i = s, e do a = a+i end print(a) end

Lua- beginner guide

Lua
4 years ago
-- Different types -- local x = 10 --number local name = "john doe" --string local isAlive = false -- boolean local a = nil --no value or invalid value print(type(name)) print(type(x)) print(type(5.8)) print(type(isAlive))

Lua- Escape Char

Lua
4 years ago
-- 常用的跳脫字元 -- \" : 雙引號 -- \' : 單引號 -- \n : 換行 -- \r : 回車 -- \\ : 倒斜線 -- \[ -- \]

Lua- String

Lua
4 years ago
-- 字串String -- 單引號 string1 = 'Lua' -- 雙引號 string2 = "Introduction" print(string1, string2)

Lua- Table

Lua
4 years ago
-- Lua 唯⼀的數據結構 "Table", 可創造出不同的類型 -- 指定具體的key時,table為'字典'。 -- myTable1 = { key1 = value1, key2 = value2, ...} -- table沒有key時,table為'數組'。 -- myTable2 = {"value1", "value2", ...} mytable = {1, 2, 5} print(mytable[3])