Lua 语法学习记录一

    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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值