java中/与%的区别,取整(/)和求余(%)

System.out.println(3/5)  = 0;
System.out.println(2/5)  = 0;
System.out.println(4/5)  = 0;


System.out.println(6/5)  = 1;
System.out.println(7/5)  = 1;
System.out.println(8/5)  = 1;
System.out.println(11/5)  = 2;
System.out.println(12/5)  = 2;
System.out.println(13/5)  = 2;

  结果等于得到的整数(商的整数);取整

System.out.println(17%5) = 2;
System.out.println(16%5) = 1;
System.out.println(13%5) = 3;
System.out.println(8%5) = 3;
System.out.println(7%5) = 2;

  结果等于余数(剩余多少)

System.out.println(2%5) = 2;
System.out.println(7%8) = 7;
System.out.println(6%8) = 6;
5%5=0;  10%5=0;

  求余(取模)有个规律就是:左边小于右边,结果为左边,左边大于右边,看余数

posted @ 2018-01-15 19:43  Johnson718  阅读(76718)  评论(0编辑  收藏  举报