Java 中 int 的取模(% 运算符) · Visual lesson · 图解

17 % 5 in Java
Java 中的 17 % 5

1. Watch the jumps

1. 看数轴跳跃

17 / 5 = 3 (3 jumps of 5), 17 % 5 = 2 (where you land) · / and %

跳了 3 次、每次 5,商是 3;最后停在 2,这就是余数。

2. Step through the code

2. 逐行执行代码

1int a = 17;
2int d = 5;
3int r = a % d;
4System.out.println(r);
Variables · 变量
Console · 控制台

3. The remainder rule

3. 余数关系

17 = 3 × 5 + 2
17 = 3 × 5 + 2
→
The leftover is 2
剩下的是 2
→
17 % 5 = 2
所以 17 % 5 = 2