Cocos2d-Lua之运算符
来源:互联网 发布:如何导入整站数据库 编辑:程序博客网 时间:2024/05/29 03:31
一、二元运算符
+ - * / % ^
^是指数运算。
这里就说一下对于浮点数向上,向下取整和四舍五入取值的方法:
print(7 / 2)print(math.floor(7 / 2))print(math.ceil(7 / 2))print(math.round(7 / 2))
输出结果:
可以看到math.floor是向下取整,math.ceil是向上取整,math.round是四舍五入取值。
二、关系运算符
> >= < <= == ~=
这里注意Lua的不等号是“~=”,而不是以往编程语言的“!=”
不同类型之间只能做“==”或者“~=”运算,不能做> >= < <=运算
a = 5c = "true"print(a == c)print(a ~= c)
输出结果:
如果:
print(a < c)
这样就会报错,不同类型是不允许比较大小的,同样没有意义的大小比较也会出错,比如:
print(false > true)
这样的运算完全没有意义。
三、逻辑运算符
and or not
a = 5b = 7c = 9print(a < b and b < c)print(a > b or b > c)print(not (a > b))
输出结果:
四、三目运算符
a = b > c and b or c
这个三目运算符和以往常用的三目运算符:
a = b > c ? b : c
这两者的意思是一样一样的。
五、一目运算符
这是一个求长度的运算符:
a = "hello"b = {1, 2, 3, 4, nil, 5}c = {1, 2, 3, 4, nil, 5, nil}print(#a)print(#b)print(#c)
这里的“#”就是长度运算符。
输出结果:
注意对于变量b这个数组,包括nil在内。对于c这个数组,因为是以nil结尾,所以系统会找到第一次出现nil的地方作为结尾,故这里求得的c的长度就是4。
0 0
- Cocos2d-Lua之运算符
- Lua 基础之运算符
- Lua基础之运算符
- Cocos2d-Lua之函数
- Cocos2d-Lua之赋值
- Cocos2d-Lua之table
- Cocos2d-Lua之string
- Cocos2d-Lua之随机数
- Cocos2d-x 之Lua初学
- Cocos2d-X之LUA笔记
- Cocos2d-Lua之循环语句
- Cocos2d-Lua之math库
- Cocos2d-Lua之文件操作
- Cocos2d-Lua之异常处理
- Cocos2d-Lua之协同程序
- Cocos2d-Lua之闭包
- lua运算符优先级
- lua中的运算符
- commons-lang工具类用法
- JSP中的MIME
- Ubuntu16.04安装/卸载Atom
- Linux系统调用
- Convert Sorted Array to Binary Search Tree 根据数组建立二分搜索树
- Cocos2d-Lua之运算符
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片的优缺点比较
- MFC 六大机制 (0) 序章
- 差分约束系统
- android 自定义控件(二)
- 【C/C++】深入预处理
- 通过跳绳瘦下来是一种怎么样的体验?
- 自定义属性获取
- Mat