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

内容摘要
区别:%是求模运算符,/是除运算符。%是求余运算,/是普通的除号。实例:System.out.println(3/5) = 0;
System.out.println(2/5) = 0;
System.out.println(4/5) = 0;
System.out.pr
文章正文

区别:

%是求模运算符,/是除运算符。%是求余运算,/是普通的除号。

实例:

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入门教程

代码注释
[!--zhushi--]

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!