Rain and Rainbow bet on what 17 % 5 would print, turning into a full lesson on modulo.

Rainbow
Rain! It's almost midnight — what are you still doing at the computer?
Rain
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
2
Rainbow
Look at the number line — each jump skips 5 steps.
Rain
17 divided by 5 is 3 with something left over — I'd say it's 2.
Rainbow
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
7
Rainbow
See? 127 seconds is 2 complete minutes and 7 leftover.
Rain
2 and 7 — no problem. It's like taking off full groups.
Rainbow
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
-2
Rainbow
See the number line — the jump lands on the negative side.
Rain
Hmm, the sign follows the left number. So -17 % 5 is -2.
Rainbow
Good. What about 17 % -5?
Rain
Left number is positive, so the result should be positive 2.
Rainbow
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 1
Rainbow
See how 17 items grouped by 5 leaves 2 behind?
Rain
That's cool — it goes 0, 1, 2, 3, 0, 1. It's like a clock that resets.
Rainbow
One more — a real-world scenario. A dial needs to land between 0 and 11.

A dial that won't stay at 7

  • r is a random number between 0 and 99.
  • r % 12 gives 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
6
Rainbow
Watch the while loop subtract 5 again and again.
Rain
Let me think... r is between 0 and 99, so r % 12 should be between 19 and 91.
Rainbow
Rain, hold on. 19 and 91 are bigger than 12. The remainder after dividing by 12 can never be bigger than 11.
Rain
Oh... wait. So the range is 0 to 11? Not 0 to 99?
Rainbow
Right. % 12 forces the answer into the range 0–11, no matter how big r is.
Rain
Got it. So the dial would land on any number from 0 to 11, not stuck at 7.
Rainbow
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 % 0 compiles fine but throws ArithmeticException at 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 zero
Rain
It throws an error — you can't divide by zero, right?
Rainbow
Exactly. So remember: % gives you the remainder, the sign follows the left number, and never use 0 as the divisor.
Rain
Alright, I've got it now. Modulo is just the leftover after taking away complete groups.
Rainbow
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, then 7.
  • Explain the rule for negative numbers: What does this code print? → -2.
  • Recognize a common use of %: Name a real situation where a % b is useful → checking whether a number is odd or even.
  • Avoid division-by-zero errors: What happens if pageSize is 0 in int pageCount = totalItems % pageSize;? → The program throws an error because a divisor cannot be zero.

Code and examples

1
System.out.println(17 % 5);
1
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int seconds = 127;
System.out.println(seconds / 60);
System.out.println(seconds % 60);
}
}
1
2
int result = -17 % 5;
System.out.println(result);

Going further

  • % can wrap an array index around: index % array.length brings it back to the start.
  • Math.floorMod(a, b) always returns a non-negative result, regardless of the sign of a.
  • Floating-point numbers also support %: double r = 5.7 % 2.0; gives 1.7.
  • [[Java int-整数除法]]
  • [[Java 泛型-基础]]
  • [[Java 零基础-环境搭建与Hello World]]

Rain 和 Rainbow 打赌 17 % 5 会输出什么,结果变成了一堂完整的取模课。

Rainbow
Rain!都快半夜了,你还不睡,又在电脑上搞什么?
Rain
就一件事——System.out.println(17 % 5); 会输出什么?我感觉我应该知道的。

取模运算符

  • % 给出除法后的余数。
  • 17 % 5 → 17 除以 5 等于 3 余 2。
  • % 读作 "取模" 或 "余数"。
System.out.println(17 % 5);
输出
2
Rainbow
看数轴——每次跳 5 步。
Rain
17 除以 5 商 3 余点东西……应该是 2。
Rainbow
对。但咱们打赌吧——你猜对了我就洗碗。
好,这题呢——

计算正整数余数

  • 127 / 60 = 2(完整分钟数)
  • 127 % 60 = 7(剩余秒数)
  • % 去掉完整的组,只留下余数。
int seconds = 127;
System.out.println(seconds / 60);
System.out.println(seconds % 60);
输出
2
7
Rainbow
看到了吗?127 秒是 2 分钟整,还剩 7 秒。
Rain
2 和 7,没问题——就像去掉完整的组。
Rainbow
现在来难的——负数。这题输出什么?

Java 的负数余数

  • Java 的 % 保留左边数字(被除数)的符号。
  • -17 % 5 → 结果是 -2,不是 2。
  • 符号跟随 % 左边的数。
int result = -17 % 5;
System.out.println(result);
输出
-2
Rainbow
看数轴——跳到了负数那一边。
Rain
嗯,符号跟左边的数走。所以 -17 % 5 是 -2。
Rainbow
好。那 17 % -5 呢?
Rain
左边的数是正数,所以结果应该是正 2。
Rainbow
对。而且 % 还特别适合做循环。看这个:

用 % 实现循环

  • % 天然地形成循环。
  • 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
Rainbow
看到 17 个物品每 5 个一组,剩下 2 个了吗?
Rain
酷——它会 0、1、2、3、0、1 这样循环。就像一个会重置的时钟。
Rainbow
还有一道——实际场景。一个拨号盘要落在 0 到 11 之间。

停不下来的拨号盘

  • r 是 0 到 99 之间的随机数。
  • r % 12 给出余数——0 到 11。
  • 它不可能每次都得到 7,因为随机数每次都在变。
int dial = 7;
int r = 42;
dial = r % 12;
System.out.println(dial);
输出
6
Rainbow
看 while 循环不断减 5。
Rain
让我想想……r 是 0 到 99 之间的数,所以 r % 12 应该在 19 到 91 之间吧。
Rainbow
Rain 等一下。19 和 91 都比 12 还大。除以 12 的余数不可能比 11 大。
Rain
哦……等等。所以范围是 0 到 11?不是 0 到 99?
Rainbow
对。% 12 强制结果落在 0 到 11 之间,不管 r 多大。
Rain
懂了。所以拨号盘会随机落在 0 到 11 之间,不会卡在 7。
Rainbow
对。这就是你用它来循环遍历游戏地图的方式。
最后一个问题——% 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
Rain
会报错——不能除以 0,对吧?
Rainbow
对。记住:% 给余数,符号跟左边的数走,永远别用 0 做除数。
Rain
好了,我现在明白了。取模就是把完整组都去掉后剩下的部分。
Rainbow
这就是你的定义了。现在去睡吧——该你洗碗了。

自测

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
2
3
4
5
6
7
public class Main {
public static void main(String[] args) {
int seconds = 127;
System.out.println(seconds / 60);
System.out.println(seconds % 60);
}
}
1
2
int result = -17 % 5;
System.out.println(result);

延伸

  • % 也可以用于数组索引循环:index % array.length 让索引回到开头
  • Math.floorMod(a, b) 始终返回非负结果,与 a 的正负无关
  • 浮点数也可以用 %:double r = 5.7 % 2.0; 结果是 1.7

相关