Java 中的模运算符
原文:https://www.studytonight.com/java-examples/modulo-operator-in-java
模运算符用于求两个数除法的余数。它也被称为余数运算符。百分比符号(%)用于表示该运算符。该运算符的基本语法如下所示。
Dividend % Divisor
让我们看一些模运算符的例子。
模运算符示例
让我们对整数和浮点数使用模运算符,并查看其输出。我们也可以对负数使用模运算符。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
System.out.println("10 % 3 = " + 10 % 3);
System.out.println("3 % 10 = " + 3 % 10);
System.out.println("10.0 % 3.0 = " + 10.0 % 3.0);
System.out.println("10.0 % 3 = " + 10.0 % 3);
System.out.println("-10 % 3 = " + -10 % 3);
}
}
10% 3 = 1 3% 10 = 3 10.0% 3.0 = 1.0 10.0% 3 = 1.0 -10% 3 =-1
需要模运算符
需要模运算符,因为使用简单除法运算符时,我们可能会丢失余数部分。如果被除数和除数都是整数,那么我们可以很容易地计算出如下所示的余数。这种情况的一个例子如下所示。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
int dividend = 10;
int divisor = 3;
int remainder = dividend - (divisor * (dividend/divisor));
System.out.print("The remainder is: " + remainder);
}
}
余数为:1
当被除数或除数不是整数时
但如果被除数或除数有小数点,那么商也将是小数,我们就找不到余数了。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
double dividend = 10;
int divisor = 3;
double remainder = dividend - (divisor * (dividend/divisor));
System.out.print("The remainder is: " + remainder);
}
}
余数为:0.0
然而,模运算符仍然可以找到正确的余数。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
double dividend = 10;
int divisor = 3;
double remainder = dividend % divisor;
System.out.print("The remainder is: " + remainder);
}
}
余数为:1.0
模运算符的算术异常
模运算符,就像除法运算符一样,如果除数为零,抛出ArithmeticException
。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
System.out.print("The remainder is: " + 10 % 0);
}
}
线程“main”中的异常 Java . lang . arithmeticexception:/by 零 在代码片段处。modulooterardemo . main(modulooterardemo . Java:7)
模运算符的常见用例
查找偶数
模运算符最常见的用法之一是检查一个数是奇数还是偶数。一个数是偶数,即使我们把一个数除以二,余数为零。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
int[] arr = {7, 5, 2, 4, 6, 19, 18, 25, 22};
for(int i=0; i<arr.length; i++)
{
int num = arr[i];
if((num % 2) == 0)
System.out.println(num + " is even.");
else
System.out.println(num + " is NOT even.");
}
}
}
7 不是偶数。 5 不是偶数。 二是偶数。 4 是偶数。 6 是偶数。 19 不是偶数。 18 是偶数。 25 不是偶数。 22 是偶数。
将数字限制在一定范围内
如果我们想把一个数限制在某个范围内,我们可以使用模运算符。当数小于极限时,模运算符返回数本身。如果该数大于或等于极限,运算符将返回余数。这非常常用于维护循环数组中的索引。
例如,如果我们有一个月,其中第一天发生在星期一,我们需要找到给定一个月中某一天的名称。在模运算符的帮助下,我们将以循环的方式遍历日名数组。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int dayOfTheMonth1 = 21;
String dayName1 = dayNames[(dayOfTheMonth1 - 1) % 7];
int dayOfTheMonth2 = 12;
String dayName2 = dayNames[(dayOfTheMonth2 - 1) % 7];
System.out.println(dayOfTheMonth1 + " occurs on " + dayName1);
System.out.println(dayOfTheMonth2 + " occurs on " + dayName2);
}
}
21 发生在周日 12 发生在周五
让一个数完全被另一个数整除
假设我们有两个数字,X 和 Y,我们想从 X 中减去最小值,使它完全被 Y 整除。这个最小值将是我们除以它们后得到的余数。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
int X = 291;
int Y = 17;
int remainder = X % Y;
X = X - remainder;
System.out.println("The minimum number to subtract is: " + remainder);
System.out.println("The number after subtraction is: " + X);
}
}
最小减法数为:2 减法后的数为:289
获取整数的单个数字
我们可以使用模和除运算符来打印数字的每个数字。我们可以通过将一个数除以 10 并取余数来找到它的最后一位。然后我们将这个数字除以 10,去掉最后一个数字。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
int num = 7912064;
while(num > 0)
{
int lastDigit = num % 10;
num = num / 10;
System.out.print(lastDigit + " ");
}
}
}
4 6 0 2 1 9 7
每隔 n 次重复某事
假设我们有一个 for 循环,将变量的值从 1 增加到 30。在循环的每 5 次迭代中,我们需要打印一条语句。模运算符可以轻松实现这一点。
public class Demo
{
public static void main(String[] args) throws JSONException
{
for(int i = 1; i <= 30; i++)
{
if(i % 5 == 0)
System.out.println("Do Something");
}
}
}
做点什么 做点什么 做点什么 做点什么 做点什么 做点什么
将秒转换为分钟和秒
我们可以使用模运算符和除法运算符将秒转换为分钟和秒。将总秒数除以 60 会得到分钟数,余数会得到剩余的秒数(< 60)。
public class ModuloOperatorDemo
{
public static void main(String[] args)
{
int seconds = 401;
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
System.out.print(seconds + " seconds is equal to " + minutes +" minutes " + remainingSeconds + " seconds");
}
}
401 秒等于 6 分 41 秒
摘要
模运算符是一个简单的运算符,它返回两个数相除后的余数。如果除数为零,则返回ArthimeticException
,因为不允许除以零。这个操作符有很多不同的用例,我们在本教程中讨论了其中的一些。