java中“/”与“%”的区别

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;

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

推荐教程:java入门教程

以上就是java中“/”与“%”的区别的详细内容,更多请关注其它相关文章!