Instead of the modulo operator, which has slightly different semantics, for non-negative integers, you can use the remainder operator %. For your exact example:
if ((a % 2) == 0)
{
isEven = true;
}
else
{
isEven = false;
}
This can be simplified to a one-liner:
isEven = (a % 2) == 0;
Answer from Cody Hatch on Stack Overflowmodulo - What's the syntax for mod in Java? - Stack Overflow
How does the modulus operator work?
Beginner Java: Using the Remainder Operator
How to find remainder without using modulo operator %?
Videos
Instead of the modulo operator, which has slightly different semantics, for non-negative integers, you can use the remainder operator %. For your exact example:
if ((a % 2) == 0)
{
isEven = true;
}
else
{
isEven = false;
}
This can be simplified to a one-liner:
isEven = (a % 2) == 0;
Here is the representation of your pseudo-code in minimal Java code;
boolean isEven = a % 2 == 0;
I'll now break it down into its components. The modulus operator in Java is the percent character (%). Therefore taking an int % int returns another int. The double equals (==) operator is used to compare values, such as a pair of ints and returns a boolean. This is then assigned to the boolean variable 'isEven'. Based on operator precedence the modulus will be evaluated before the comparison.
I apologise in advance if this is a stupid question, but I've looked everywhere and every explanation I've found has been way above my pay grade.
I've been studying java for a grand total of one week. My lecturer gave us notes that cover the modulus, but after reading them I'm still none the wiser. The examples he gives don't seem consistent and I'm just getting more and more confused.
The exercise involves trying convert 174 pounds into stone and pounds using the modulus, but I honestly have no idea where the modulus goes or how it works, or what numbers I'm supposed to put where. I'd be grateful for literally any explanation at this point.