1 package jinlai.ding.com.javajiehuo; 2 3 import java.math.BigDecimal; 4 5 public class IsOld { 6 7 /** 8 * 判断奇数((计算一个数字是否奇数)) 9 * @param args 10 */ 11 public static void main(String[] args) { 12 //谜题1:奇数性 13 System.out.println(isOld(-3)); 14 System.out.println(isOldYes(-3)); 15 //谜题2:找零时刻 16 System.out.println(2.00 - 1.10); 17 System.out.println((200 - 110) + "cents"); 18 System.out.println(new BigDecimal("2.00").subtract(new BigDecimal("1.10"))); 19 //谜题3:长整数 20 System.out.println(longDivision()); 21 System.out.println(longDivisionL()); 22 //谜题4:初级问题 23 System.out.println(12345 + 54321); 24 System.out.println(12345 + 5432l); 25 System.out.println(12345 + 5432L); 26 //谜题5:十六进制的趣事 27 System.out.println(Long.toHexString(0x100000000L + 0xcafebabe)); 28 System.out.println(Long.toHexString(0x100000000L + 0xcafebabeL)); 29 30 } 31 32 /** 33 * 错误的判断方法 34 * @param i 35 * @return 36 */ 37 public static boolean isOld(int i){ 38 return i % 2 ==1; 39 } 40 41 /** 42 * 正确的判断方法 43 * @param i 44 * @return 45 */ 46 public static boolean isOldYes(int i){ 47 return (i & 1) != 0; 48 } 49 /** 50 * 微妙除以毫秒 溢出计算 51 * @return 52 */ 53 public static long longDivision(){ 54 final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; 55 final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; 56 return MICROS_PER_DAY / MILLIS_PER_DAY; 57 } 58 /** 59 * 微妙除以毫秒 不溢出计算 60 * @return 61 */ 62 public static long longDivisionL(){ 63 final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000; 64 final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000; 65 return MICROS_PER_DAY / MILLIS_PER_DAY; 66 } 67 68 }
原文:http://www.cnblogs.com/dotopd/p/5119986.html