The modulo operator is an operator
mod
, denoted by the symbol %
. This operator returns the remainder after dividing the first operand by the second. The operator mod
" %
" in Java works not only with integers (such as: byte/int/short/long
), but also with floating point (such as: float/double
) numbers/types. The following program illustrates the operation of this operator:
package com.l2cccp.work;
public class Mod
{
public static void main(String args[])
{
int i = 17; // Integers
double d = 17.3; // floating point
System.out.println("i mod 10 = " + i % 10);
System.out.println("d mod 10 = " + d % 10);
}
}
When you run this program, you will get the following output:
i mod 10 = 7
d mod 10 = 7.300000000000001
We figured out the work of this operator, but how to use it. As an option for declining words depending on the number. Since I work as a developer of a java game emulator, it is very important for me that many words in messages are inflected. Eg:
- You have been playing for 1 day already .
- You have been playing for 2 days now .
- You have been playing for 5 days now .
package com.l2cccp.work;
public class Mod
{
public static void main(String args[])
{
int[] day= new int[] { 1, 2, 5 };
System.out.println("You're already playing" + day[0] + " " + declension(day[0]));
System.out.println("You're already playing" + day[1] + " " + declension(day[1]));
System.out.println("You're already playing" + day[2] + " " + declension(day[2]));
}
public static String declension(int count)
{
String one = "day";
String two = "of the day";
String five = "days";
if(count > 100)
count %= 100;
if(count > 20)
count %= 10;
switch(count)
{
case 1:
return one;
case 2:
case 3:
case 4:
return two;
default:
return five;
}
}
}
When you run this program, you will get the following output:
Вы играете уже 1 день
Вы играете уже 2 дня
Вы играете уже 5 дней
Everything works as we need, but what if we need to get several declensions in one result? Eg:
- You have been playing for 1 day and 1 hour .
- You have been playing for 2 days and 4 hours .
- You have been playing for 5 days 9 hours .
package com.l2cccp.work;
public class Mod
{
public static void main(String args[])
{
int[] day = new int[] { 1, 2, 5 };
int[] hour = new int[] { 1, 4, 9 };
System.out.println("You're already playing" + day[0] + " " + declension(day[0], "Days") + " And " + hour[0] + " " + declension(hour[0], "Hour"));
System.out.println("You're already playing" + day[1] + " " + declension(day[1], "Days") + " And " + hour[1] + " " + declension(hour[1], "Hour"));
System.out.println("You're already playing" + day[2] + " " + declension(day[2], "Days") + " And " + hour[2] + " " + declension(hour[2], "Hour"));
}
public static String declension(int count, String type)
{
String one = "";
String two = "";
String five = "";
if(type.equals("Days"))
{
one = "day";
two = "of the day";
five = "days";
}
else if(type.equals("Hour"))
{
one = "hour";
two = "hours";
five = "hours";
}
if(count > 100)
count %= 100;
if(count > 20)
count %= 10;
switch(count)
{
case 1:
return one;
case 2:
case 3:
case 4:
return two;
default:
return five;
}
}
}
When you run this program, you will get the following output:
Вы играете уже 1 день и 1 час
Вы играете уже 2 дня и 4 часа
Вы играете уже 5 дней и 9 часов
Since our method is described as public " public
", it can be easily called from any package:
Mod.declension(count, type);
Actually, that's all, I hope it will be useful to someone. Good luck with learning the Java language.
GO TO FULL VERSION