java中/与%的区别
java中/与%的区别
1
2
3
4
5
6
7
8
9
10
11
|
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 ; |
结果等于得到的整数(商的整数)
1
2
3
4
5
|
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 ; |
结果等于余数(剩余多少)
1
2
3
|
System.out.println( 2 % 5 ) = 2 ; System.out.println( 7 % 8 ) = 7 ; System.out.println( 6 % 8 ) = 6 ; |
取余(取模)有个规律就是:左边小于右边,结果为左边,左边大于右边,看余数