代码: package puzz; public class Odd { public static void main(String[] args) { System.out.println("--------------------------"); System.out.println("isOdd1 : "); System.out.println(isOdd1(2)); System.out.println(isOdd1(1)); System.out.println(isOdd1(-1)); System.out.println(isOdd1(-2)); System.out.println("--------------------------"); System.out.println("isOdd2 : "); System.out.println(isOdd2(2)); System.out.println(isOdd2(1)); System.out.println(isOdd2(-1)); System.out.println(isOdd2(-2)); System.out.println("--------------------------"); System.out.println("isOdd : "); System.out.println(isOdd(2)); System.out.println(isOdd(1)); System.out.println(isOdd(-1)); System.out.println(isOdd(-2)); } // wrong method public static boolean isOdd1(int i) { return i % 2 == 1; } // OK method public static boolean isOdd2(int i) { return i % 2 != 0; } // better method public static boolean isOdd(int i) { return (i & 1) != 0; } } 结果: //-------------------------- //isOdd1 : //false //true //false //false //-------------------------- //isOdd2 : //false //true //true //false //-------------------------- //isOdd : //false //true //true //false isOdd1判断负奇数的时候出错 isOdd2可以正确判断 isOdd效率更高