local n = 4
function fact( n ) -- 求n的阶乘,递归
if n == 0 then
return 1
else
return n*fact(n-1)
end
end
print(fact(n)) -- 24
print(type("hello world")) -- string
print(type(nil)) -- nil
print(type(print)) -- function
print(type(true)) -- boolean
print(type(type)) -- function
print(type(10)) -- number
print(type(11.11)) -- number
-- 函数赋值
local func = fact
print(func(10)) -- 24
-- 字符串
local str = "one string"
local str2 = string.gsub(str, "one", "another")
print(str2) -- another string
print('\97') -- a (ASCII系统)
print("10"+1) -- 11
print("10 + 1") -- 10 + 1
-- print("hello" + 1) 错误
print(10 .. 10) -- 1010 ps: ..必须用空格分隔
-- 函数tonumber转换,同理有tostring
local str3 = "100"
print(100 == "100") -- false
print(100 == tonumber(str3)) -- true
local str4 = "abcd"
print(#str4) -- 4 ps:用#来获取长度
local x = math.pi
print(x) -- 3.1415926535898
print(x - x%0.01) -- 3.14
-- ==
-- 不同类型,肯定是 false
-- 同类型,table,userdata,函数 是作引用比较, 其他的是值比较
-- and or
print(4 and 5) -- 5
print(nil and 5) -- nil
print(4 or 5) -- 4
print(nil or 5) -- 5
print(4 or nil) -- 4
-- 三目运算 c语言中 int value = 5>4?5:4
local value = (5<4) and 5 or 4 -- ((5<4) and 5) or 4
print("value = ", value) -- 4
03-30
06-25
06-25
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交