How to correctly judge odd numbers in java Puzzles
How to correctly judge odd numbers in java Puzzles
How to judge whether a positive number is an odd number seems simple, but we often ignore negative, 0, and positive integers. Mathematical concepts are also important.
Package com.doc tor. java. puzzle;/*** @ author sdcuike ** @ time 9:08:54 on January 1, January 31, 2016 * how to correctly judge the odd number * must take into account the positive, negative, and zero situations. */Public class Oddity {public static void main (String [] args) {System. out. println (isOddWrong (5); System. out. println (isOdd (5); System. out. println (isOddFastWay (5); System. out. println (isOddWrong (-5); // In this case, the negative number will fail System. out. println (isOdd (-5); System. out. println (isOddFastWay (-5); System. out. println (-5% 2); //, the remainder of the negative number % has a positive negative number System. out. println (0% 2); //, the remainder of the negative number % is positive and negative.}/*** the remainder of the negative number % is not considered. Positive and negative signs ** @ param I * @ return */private static boolean isOddWrong (int I) {return I % 2 = 1;}/*** the correct determination is odd, non-ideologically-non-even is an odd number ** @ param I * @ return */private static boolean isOdd (int I) {return I % 2! = 0;} private static boolean isOddFastWay (int I) {return (I & 1 )! = 0 ;}}