算术运算符
程序员文章站
2022-03-19 23:40:03
算术运算符 : +(正) - +(加) - * / %(取余) 前++ 后++ 前-- 后-- +(连接符) ......
算术运算符 : +(正) - +(加) - * / %(取余) 前++ 后++ 前-- 后-- +(连接符)
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 } }