Lua 语法学习记录一

来源:互联网 发布:新加坡电信网络制式 编辑:程序博客网 时间:2024/06/05 14:38
    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

0 0
原创粉丝点击