算术运算符
算术运算符 : +(正) – +(加) – * / %(取余) 前++ 后++ 前– 后– +(连接符)
public class AriTest{ public static void main(String[] args){ int a = +5; System.out.println(a); System.out.println("-----------除-------------"); int num = 10; int num2 = num / 4; //2 System.out.println(num2); double num3 = 10 / 4; System.out.println("num3=" + num3);//2.0 double num4 = 10 / (double)4; System.out.println("num4=" + num4); double num5 = (10 * 1.0) / 4; num5 = 10 / 4 * 4; num5 = 10 * 4 / 4; System.out.println("num5=" + num5); System.out.println("-----------取余-------------"); //用途 : 1.判断一个数是否能被另一个数整除 System.out.println(0 % 3);//0 System.out.println(1 % 3);//1 System.out.println(2 % 3);//2 System.out.println(3 % 3);//0 System.out.println(4 % 3);//1 System.out.println(5 % 3);//2 System.out.println("-----------------------"); //思考 :取模的结果的正负和谁有关?和被模数有关 System.out.println(-4 % 3);//-1 System.out.println(4 % -3); System.out.println(-4 % -3);//-1 System.out.println("-----------前++ 后++ 前-- 后-- -------------"); int n = 10; int nn = n++;//先赋值(运算),再自增1 System.out.println("n=" + n + " nn=" + nn);//n=11 nn=10 int n2 = 10; int nn2 = ++n2;//先自增1,再赋值(运算) System.out.println("n2=" + n2 + " nn2=" + nn2);//n2=11 nn2=11 System.out.println("-------------------------------"); int n3 = 10; int nn3 = n3--;//先赋值(运算),再自减1 System.out.println("n3=" + n3 + " nn3=" + nn3);//n3 = 9 nn3 = 10 int n4 = 10; int nn4 = --n4;//先自减1,再赋值(运算) System.out.println("n4=" + n4 + " nn4=" + nn4);//n4 = 9 nn4 = 9 } }
相关文章