基础运算与自增自减运算
1.基础运算
int 整型
double 浮点
package day06;
public class operation {
public static void main(String[] args) {
int a=10;
int b=3;
System.out.println("a+b="+(a+b));
System.out.println("a-b="+(a-b));
System.out.println("a*b="+(a*b));
System.out.println("a/b="+((double)a/b));//强制转换
double c=10.12;
double d=3.3;
System.out.println("c+d="+(c+d));
System.out.println("c-d="+(c-d));
System.out.println("c*d="+(c*d));
System.out.println("c/d="+(c/d));
}
}
打印出:
a+b=13
a-b=7
a*b=30
a/b=3.3333333333333335
c+d=13.419999999999998
c-d=6.819999999999999
c*d=33.395999999999994
c/d=3.0666666666666664
2.模运算符
%是求两个整数相除的余数
取模运算不建议用于浮点型,结果可能不准确;
对于精度高的运算,需要使用BigDecimal类型;
package day06;
public class operation1 {
public static void main(String[] args) {
int a1=5;
int a2=2;
double b1=10.1;
double b2=3.3;
System.out.println("int a1="+a1+",a2="+a2);
System.out.println("double b1"+b1+",b2="+b2);
System.out.println("a1%a2="+(a1%a2));
System.out.println("a1%b1="+(a1%b1));
System.out.println("b1%b2="+(b1%b2));
System.out.println("b1%a2="+(b1%a2));
}
}
打印出:
int a1=5,a2=2
double b110.1,b2=3.3
a1%a2=1
a1%b1=5.0
b1%b2=0.20000000000000018
b1%a2=0.09999999999999964