mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
52d4d60a62
@ -2707,6 +2707,34 @@ public class NumberUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为奇数<br>
|
||||
*
|
||||
* @param num 被判断的数值
|
||||
* @return 是否是奇数
|
||||
* @author GuoZG
|
||||
*/
|
||||
public static boolean isOdd(int num) {
|
||||
if ((num & 1) == 1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为偶数<br>
|
||||
*
|
||||
* @param num 被判断的数值
|
||||
* @return 是否是偶数
|
||||
* @author GuoZG
|
||||
*/
|
||||
public static boolean isEven(int num) {
|
||||
if (!isOdd(num)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------- Private method start
|
||||
private static int mathSubNode(int selectNum, int minNum) {
|
||||
if (selectNum == minNum) {
|
||||
|
@ -412,4 +412,16 @@ public class NumberUtilTest {
|
||||
final Integer[] integers = NumberUtil.generateBySet(10, 100, 5);
|
||||
Assert.assertEquals(5, integers.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isOddOrEvenTest(){
|
||||
int[] a = { 0, 32, -32, 123, -123 };
|
||||
for (int i : a) {
|
||||
Console.log(i + "为奇数:" + NumberUtil.isOdd(i));
|
||||
Console.log(i + "为偶数:" + NumberUtil.isEven(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user