Lua 支持多种运算符,包括算术运算符、关系运算符、逻辑运算符等。以下是一些常用的 Lua 运算符:

算术运算符
-- 加法
local sum = 10 + 5

-- 减法
local difference = 10 - 5

-- 乘法
local product = 10 * 5

-- 除法
local quotient = 10 / 5

-- 求余
local remainder = 10 % 3

-- 乘方
local power = 2 ^ 3

关系运算符
-- 等于
local isEqual = (10 == 5)

-- 不等于
local isNotEqual = (10 ~= 5)

-- 大于
local isGreaterThan = (10 > 5)

-- 小于
local isLessThan = (10 < 5)

-- 大于等于
local isGreaterOrEqual = (10 >= 5)

-- 小于等于
local isLessOrEqual = (10 <= 5)

逻辑运算符
-- 逻辑与
local andResult = true and false

-- 逻辑或
local orResult = true or false

-- 逻辑非
local notResult = not true

字符串连接运算符
local greeting = "Hello, " .. "World!"

其他运算符
-- 连接两个表
local table1 = {1, 2, 3}
local table2 = {4, 5, 6}
local concatenatedTable = table1 .. table2

-- 长度运算符(#)
local length = #someTableOrString

这些是 Lua 中常见的运算符。你可以根据具体的需求使用这些运算符进行各种计算和比较操作。请注意 Lua 中的索引是从 1 开始的,这与一些其他编程语言不同。


转载请注明出处:http://www.pingtaimeng.com/article/detail/13698/Lua