W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Groovy語(yǔ)言支持正常的算術(shù)運(yùn)算符作為任何語(yǔ)言。以下是Groovy中提供的算術(shù)運(yùn)算符 -
運(yùn)算符 | 描述 | 例子 |
+ | 兩個(gè)操作數(shù)的加法 | 1 + 2 將得到 3 |
? | 從第一個(gè)操作數(shù)中減去第二個(gè)操作數(shù) | 2 - 1 將得到 1 |
* | 兩個(gè)操作數(shù)的乘法 | 2 * 2 將得到 4 |
/ | 分子除以分母 | 3 / 2 將得到 1.5 |
% | 模數(shù)運(yùn)算符和整數(shù)/浮點(diǎn)除法后的余數(shù) | 3%2 將得到 1 |
++ | 用于將操作數(shù)的值增加1的增量運(yùn)算符 | int x = 5; x++; x 將得到 6 |
- - | 用于將操作數(shù)的值減1的增量運(yùn)算符 | int x = 5; X - ; x 將得到 4 |
以下代碼段顯示了如何使用各種運(yùn)算符。
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--); } }
當(dāng)我們運(yùn)行上面的程序,我們將得到以下結(jié)果。可以看出,結(jié)果如從上面所示的操作符的描述所預(yù)期的。
15 -5 50 1.6 3 5 6
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: