6个回答

``````    public static boolean isEven(int num){
if((num & 0x01) == 0){
return true;
}
return false;
}
``````

``````/**
* 判断是否是奇数
* @param n 参数n
* @return n是奇数返回true，否则返回false
*/
public boolean isOddNumber(int n){
return ((n & 1)==1);
}
``````

``````@Test
public void test9() {
System.out.println("0是奇数吗？"+isOddNumber(0));
System.out.println("1是奇数吗？"+isOddNumber(1));
System.out.println("-1是奇数吗？"+isOddNumber(-1));
System.out.println("-20是奇数吗？"+isOddNumber(-20));
}
``````

``````0是奇数吗？false
1是奇数吗？true
-20是奇数吗？false
-1是奇数吗？true
``````
``````public static void main(String[] args) {
int n1 = 123;
int n2 = 234;
fun(n1);
fun(n2);
}

private static void fun(int number) {
// 将数字转换为字符串
String numberStr = String.valueOf(number);
// 截取字符串最后一个数字
char lastNumber = numberStr.toCharArray()[numberStr.length() - 1];
// 判断奇数与偶数
if (lastNumber == '1' || lastNumber == '3' || lastNumber == '5' || lastNumber == '7' || lastNumber == '9') {
System.out.println(number + "为奇数");
} else {
System.out.println(number + "为偶数");
}
}

``````

4 个月之前 回复
Soul fragments 也可以直接转化成二进制截取最后一个数字判断0还是1就好了
4 个月之前 回复

import java.util.Scanner;

public class simple {

``````public static void main(String[] args) {
while(true) {
System.out.println("请输入需要判断的数字");
Scanner s = new Scanner(System.in);
int a = s.nextInt();
double b=a/2.0;
int c=(int)b;
double d=0;
if(b-c==d)
System.out.println("偶数");
else
System.out.println("奇数");
}
}
``````

}

if((num & 1) ==0)retrun true;