目录

Show Example

Groovy语言支持普通的算术运算符作为任何语言。 以下是Groovy中可用的算术运算符 -

操作者 描述
+ 增加了两个操作数 1 + 2将给出3
从第一个减去第二个操作数 2 - 1将给出1
* 两个操作数的乘法 2 * 2将给4
/ 由分母划分的分子 3/2会给1.5
% 模数运算符和整数/浮点除法后的余数 3%2将给1
++ 增量运算符用于将操作数的值递增1

int x = 5;

X ++;

x将给出6

-- 增量运算符用于将操作数的值减1

int x = 5;

X - ;

x将给出4

以下代码段显示了如何使用各种运算符。

class Example { 
   static void main(String[] args) { 
      // Initializing 3 variables 
      def x = 5; 
      def y = 10; 
      def z = 8; 
      //Performing addition of 2 operands 
      println(x+y); 
      //Subtracts second operand from the first 
      println(x-y); 
      //Multiplication of both operands 
      println(x*y);
      //Division of numerator by denominator 
      println(z/x); 
      //Modulus Operator and remainder of after an integer/float division 
      println(z%x); 
      //Incremental operator 
      println(x++); 
      //Decrementing operator 
      println(x--);  
   } 
} 

当我们运行上述程序时,我们将得到以下结果。 可以看出,结果如上所述的操作者描述所预期的那样。

15 
-5 
50 
1.6 
3 
5 
6
↑回到顶部↑
WIKI教程 @2018