2016-06-29
+ 加法运算
- 减法
* 乘法
/ 除法
% 取模
++ 自加运算
-- 自减运算
+= 加法赋值
-=
*=
/=
%=
1 基本算术运算符
package com.java1995; /** * 基本算术运算符 * @author Administrator * */ public class Test { public static void main(String[] args){ //声明2个int类型变量 int i1=7; int i2=2; System.out.println("基本运算符演示,整数类型"); System.out.println("i1="+i1+" i2="+i2); System.out.println("i1+i2="+(i1+i2)); System.out.println("i1-i2="+(i1-i2)); System.out.println("i1*i2="+(i1*i2)); System.out.println("i1/i2="+((double)i1/i2)); System.out.println("i1%i2="+(i1%i2)); //声明2个double类型变量 double d1=8.0; double d2=2.5; System.out.println("基本运算符演示,浮点类型"); System.out.println("d1="+d1+" d2="+d2); System.out.println("d1+d2="+(d1+d2)); System.out.println("d1-d2="+(d1-d2)); System.out.println("d1*d2="+(d1*d2)); System.out.println("d1/d2="+(d1/d2)); } }
2 模运算符
% 是求2个整数相除的余数
取模运算不建议用于浮点型,结果可能不准确
对于精度高的运算,需要使用BigDecimal类型
package com.java1995; /** * 模运算符 * @author Administrator * */ public class Test2 { public static void main(String[] args){ int i1=5; int i2=2; double d1=2.4; double d2=1.1; System.out.println("int i1="+i1+"i2="+i2); System.out.println("double d1="+d1+" d2="+d2); System.out.println("i1%i2="+(i1%i2)); System.out.println("i1%d1="+(i1%d1)); System.out.println("d1%d2="+(d1%d2)); System.out.println("d1%i2="+(d1%i2)); } }
【参考资料】
时间: 2024-10-25 05:03:52