Rain and Rainbow bet on what 17 % 5 would print, turning into a full lesson on modulo.
Rain! It's almost midnight — what are you still doing at the computer?
Just this one thing — what does System.out.println(17 % 5); print? I feel like I should know.
The modulo operator
%gives the remainder after division.17 % 5→ 17 divided by 5 equals 3 with remainder 2.- Read
%as "modulo" or "remainder of".
System.out.println(17 % 5);Output
2Look at the number line — each jump skips 5 steps.
17 divided by 5 is 3 with something left over — I'd say it's 2.
Correct. But let's bet — if you're right, I'll wash the dishes tonight.
OK, what about this — what does it print?
Calculating a remainder
127 / 60 = 2(whole minutes)127 % 60 = 7(remaining seconds)%strips away the complete groups, leaving only what's left over.
int seconds = 127;
System.out.println(seconds / 60);
System.out.println(seconds % 60);Output
2
7See? 127 seconds is 2 complete minutes and 7 leftover.
2 and 7 — no problem. It's like taking off full groups.
Now for the tricky part — negative numbers. What does this print?
Negative remainders in Java
- Java's
%keeps the sign of the left number (the dividend). -17 % 5→ the answer is -2, not 2.- The sign follows the number on the left side of
%.
int result = -17 % 5;
System.out.println(result);Output
-2See the number line — the jump lands on the negative side.
Hmm, the sign follows the left number. So -17 % 5 is -2.
Good. What about 17 % -5?
Left number is positive, so the result should be positive 2.
Exactly. And % is also useful for creating cycles. Watch:
Using % for cycles
%naturally creates cycles.0 % 4, 1 % 4, 2 % 4, 3 % 4, 4 % 4, 5 % 4→0, 1, 2, 3, 0, 1- This pattern repeats forever — perfect for circular buffers, round-robin, and index wraparound.
int[] a = {0,1,2,3,4,5};
for (int i : a) System.out.print(i % 4 + " ");Output
0 1 2 3 0 1See how 17 items grouped by 5 leaves 2 behind?
That's cool — it goes 0, 1, 2, 3, 0, 1. It's like a clock that resets.
One more — a real-world scenario. A dial needs to land between 0 and 11.
A dial that won't stay at 7
ris a random number between 0 and 99.r % 12gives a remainder — 0 to 11.- It will never be 7 reliably, because the random part changes every time.
int dial = 7;
int r = 42;
dial = r % 12;
System.out.println(dial);Output
6Watch the while loop subtract 5 again and again.
Let me think... r is between 0 and 99, so r % 12 should be between 19 and 91.
Rain, hold on. 19 and 91 are bigger than 12. The remainder after dividing by 12 can never be bigger than 11.
Oh... wait. So the range is 0 to 11? Not 0 to 99?
Right.
% 12 forces the answer into the range 0–11, no matter how big r is.Got it. So the dial would land on any number from 0 to 11, not stuck at 7.
Exactly. And that's how you'd use it to wrap around a game map.
One last question — what happens if you do
% 0?Division by zero
- You cannot divide or take modulo by zero.
10 % 0compiles fine but throwsArithmeticExceptionat runtime.- Always check that the divisor is non-zero before using
%.
int a = 10;
int b = 0;
try {
int result = a % b;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}Output
Error: / by zeroIt throws an error — you can't divide by zero, right?
Exactly. So remember:
% gives you the remainder, the sign follows the left number, and never use 0 as the divisor.Alright, I've got it now. Modulo is just the leftover after taking away complete groups.
That's your definition. Now go to sleep — you're going to wash those dishes.
Check yourself
What does 23 % 4 print?
3 (23 divided by 4 is 5 with remainder 3)
What is the sign of -10 % 3 in Java?
Negative — the sign follows the left number (-10)
Quick reference
Key points
- Explain what
%does:17 % 5 = 2. - Find a positive remainder: Read the program and write the output, line by line →
2, then7. - Explain the rule for negative numbers: What does this code print? →
-2. - Recognize a common use of
%: Name a real situation wherea % bis useful → checking whether a number is odd or even. - Avoid division-by-zero errors: What happens if
pageSizeis0inint pageCount = totalItems % pageSize;? → The program throws an error because a divisor cannot be zero.
Code and examples
1 | System.out.println(17 % 5); |
1 | public class Main { |
1 | int result = -17 % 5; |
Going further
%can wrap an array index around:index % array.lengthbrings it back to the start.Math.floorMod(a, b)always returns a non-negative result, regardless of the sign ofa.- Floating-point numbers also support
%:double r = 5.7 % 2.0;gives1.7.
Related
- [[Java int-整数除法]]
- [[Java 泛型-基础]]
- [[Java 零基础-环境搭建与Hello World]]
Rain 和 Rainbow 打赌 17 % 5 会输出什么,结果变成了一堂完整的取模课。
Rain!都快半夜了,你还不睡,又在电脑上搞什么?
就一件事——
System.out.println(17 % 5); 会输出什么?我感觉我应该知道的。取模运算符
%给出除法后的余数。17 % 5→ 17 除以 5 等于 3 余 2。%读作 "取模" 或 "余数"。
System.out.println(17 % 5);输出
2看数轴——每次跳 5 步。
17 除以 5 商 3 余点东西……应该是 2。
对。但咱们打赌吧——你猜对了我就洗碗。
好,这题呢——
计算正整数余数
127 / 60 = 2(完整分钟数)127 % 60 = 7(剩余秒数)%去掉完整的组,只留下余数。
int seconds = 127;
System.out.println(seconds / 60);
System.out.println(seconds % 60);输出
2
7看到了吗?127 秒是 2 分钟整,还剩 7 秒。
2 和 7,没问题——就像去掉完整的组。
现在来难的——负数。这题输出什么?
Java 的负数余数
- Java 的
%保留左边数字(被除数)的符号。 -17 % 5→ 结果是 -2,不是 2。- 符号跟随
%左边的数。
int result = -17 % 5;
System.out.println(result);输出
-2看数轴——跳到了负数那一边。
嗯,符号跟左边的数走。所以 -17 % 5 是 -2。
好。那 17 % -5 呢?
左边的数是正数,所以结果应该是正 2。
对。而且 % 还特别适合做循环。看这个:
用 % 实现循环
%天然地形成循环。0 % 4到5 % 4→0, 1, 2, 3, 0, 1- 这个模式永远重复——非常适合循环缓冲区、轮转和索引回绕。
int[] a = {0,1,2,3,4,5};
for (int i : a) System.out.print(i % 4 + " ");输出
0 1 2 3 0 1看到 17 个物品每 5 个一组,剩下 2 个了吗?
酷——它会 0、1、2、3、0、1 这样循环。就像一个会重置的时钟。
还有一道——实际场景。一个拨号盘要落在 0 到 11 之间。
停不下来的拨号盘
r是 0 到 99 之间的随机数。r % 12给出余数——0 到 11。- 它不可能每次都得到 7,因为随机数每次都在变。
int dial = 7;
int r = 42;
dial = r % 12;
System.out.println(dial);输出
6看 while 循环不断减 5。
让我想想……r 是 0 到 99 之间的数,所以 r % 12 应该在 19 到 91 之间吧。
Rain 等一下。19 和 91 都比 12 还大。除以 12 的余数不可能比 11 大。
哦……等等。所以范围是 0 到 11?不是 0 到 99?
对。
% 12 强制结果落在 0 到 11 之间,不管 r 多大。懂了。所以拨号盘会随机落在 0 到 11 之间,不会卡在 7。
对。这就是你用它来循环遍历游戏地图的方式。
最后一个问题——
% 0 会怎样?除零错误
- 不能除以 0 或取 0 的模。
10 % 0编译没问题,但运行时抛出ArithmeticException。- 使用
%之前始终要检查除数不为 0。
int a = 10;
int b = 0;
try {
int result = a % b;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}输出
Error: / by zero会报错——不能除以 0,对吧?
对。记住:% 给余数,符号跟左边的数走,永远别用 0 做除数。
好了,我现在明白了。取模就是把完整组都去掉后剩下的部分。
这就是你的定义了。现在去睡吧——该你洗碗了。
自测
23 % 4 会输出什么?
3(23 除以 4 商 5 余 3)
Java 中 -10 % 3 的符号是什么?
负数——符号跟随左边的数(-10)
要点速查
要点
- 能解释 % 的基本作用:17 % 5 = 2
- 能判断正整数取模结果:Read this program and write what appears on the screen, line by line. / 这段程序依次打印出什么? → 2 / 7
- 能说明负数参与取模的规则:这段代码输出什么? → -2
- 能识别 % 的常见用途:你能想到 a % b 在什么场景下有用吗?举一个实际的例子。 → 判断一个数是奇数还是偶数
- 能避免取模的除零错误:小明写了一个分页功能:int pageCount = totalItems % pageSize;。如果他不小心把 pageSize 设为 0,会发生什么? → 会报错,因为0不能成为除数
代码与例句
1 | System.out.println(17 % 5); |
1 | public class Main { |
1 | int result = -17 % 5; |
延伸
- % 也可以用于数组索引循环:index % array.length 让索引回到开头
- Math.floorMod(a, b) 始终返回非负结果,与 a 的正负无关
- 浮点数也可以用 %:double r = 5.7 % 2.0; 结果是 1.7