Lua - 算术运算符

  • 简述

    下表显示了Lua语言支持的所有算术运算符。假设变量A 容纳 10 和变量 B 持有 20,然后 -
    操作符 描述 例子
    + 添加两个操作数 A + B 等于 30
    - 从第一个操作数中减去第二个操作数 A - B 会等于 -10
    * 将两个操作数相乘 A * B 等于 200
    / 分子除以分数 B/A 会等于 2
    % 模运算符和整数除法后的余数 B % A 会等于 0
    ^ 指数运算符取指数 A^2 会等于 100
    - 一元 - 运算符充当否定 -A 会等于 -10
  • 例子

    尝试以下示例以了解 Lua 编程语言中可用的所有算术运算符 -
    
    a = 21
    b = 10
    c = a + b
    print("Line 1 - Value of c is ", c )
    c = a - b
    print("Line 2 - Value of c is ", c )
    c = a * b
    print("Line 3 - Value of c is ", c )
    c = a / b
    print("Line 4 - Value of c is ", c )
    c = a % b
    print("Line 5 - Value of c is ", c )
    c = a^2
    print("Line 6 - Value of c is ", c )
    c = -a
    print("Line 7 - Value of c is ", c )
    
    当您执行上述程序时,它会产生以下结果 -
    
    Line 1 - Value of c is  31
    Line 2 - Value of c is  11
    Line 3 - Value of c is  210
    Line 4 - Value of c is  2.1
    Line 5 - Value of c is  1
    Line 6 - Value of c is  441
    Line 7 - Value of c is  -21