模运算符的用途
模运算符有哪些用途?我知道它计算除法的余数,所以我真的想问余数有什么用处?
到目前为止,我已经用它来检查数字是否为偶数以及表格上的交替颜色。
What are some uses of the modulus operator? I know that it calculates the remainder in division so really I am asking what uses does the remainder have?
So far I have used it to check if a number was even and alternate colors on a table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
最基本的使用
注意:lang使用Java
The most basic use
Note: lang used Java
通过每隔多次迭代打印一条消息来获取长时间运行循环中的进度指示。
Getting an indication of progress in a long running loop by printing a message once every so many iterations.
13425 m
是13425 / 1000 km 和 13425 % 1000 m
=13 km 和 425 m
rand() % (HIGH - LOW) + LOW
生成 HIGH 和 LOW13425 m
is13425 / 1000 km and 13425 % 1000 m
=13 km and 425 m
rand() % (HIGH - LOW) + LOW
to generate a random number between HIGH and LOW72 分钟模 60 = 整点过 12 分钟
72 minutes modulo 60 = 12 minutes past the hour
按位计算,包括条件检查。
Bitwise calculations, including conditional checking.
中国算术(这是首选的术语吗,老兄?)
Chinese arithmetic (is that the preferred nomenclature, dude?)
模运算符是时钟算术中最重要的运算符。
The modulus operator is the single-most important operator in Clock Arithmetic.
它通常用于检查一个数字是否可以被另一个数字整除。
或
如果 mod 运算的结果为 0,则被除数(数字)可被除数整除。
您可以利用此功能对表数据执行“钢琴键”样式的交替行着色,或者每 X 行打印新的列标题,或者您拥有的其他操作。
It's generally used to check if one number is evenly divisible by another.
or
If the result of a mod operation is 0, the dividend (number) is evenly divisible by the divisor.
You can take advantage of this to do things like "piano-keys" style alternate-row shading on table data, or printing new column headings every X number of rows, or what have you.
编程 101 示例是调制数据的行颜色:
确定数字是奇数还是奇数:
A programming 101 exapmle would be to modulate row colors for data:
Determine if a number is evan or odd: