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;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…