Java Notes: Some Tricky Problems in Java
来源:互联网 发布:淘客cms微信系统 编辑:程序博客网 时间:2024/06/05 23:07
1/ About the modulus:
对于求余运算需要注意,当被除数是一个负数的时候,余数永远都是负的。所以你判断是否为奇数的条件不能是:x % 2 == 1,而是 x % 2 != 0。
2/ 如何提取出一个数的个、十...位(从低到高)
public Class Example { public static void main(String[] args) { int x = Integer.parseInt(args[0]); while (x != 0) { int digit = Math.abs(x % 10); // 如果不加math.abs的话如果被余数是负的,结果都是负数来的 System.out.println(digit); x = x / 10; } }}
3/ Keep in mind of overflow problem
如果int到了最大的2^30次方,不能再乘以2(右移一位)了。强行再右移的话,会瞬间变为最小的int,值是-2^31次方。下面的程序就有这样的问题:
public class Example {public static void main(String[] args) {int x = Integer.parseInt(args[0]);boolean answer = false;int p = 1;while (p <= x) {System.out.println("Testing" + p);if (p == x) {answer = true;}p = p * 2;}System.out.println(answer);}}
上述程序尝试测试一个数是否是2的倍数。然而,当所给的参数大于int的2^31 - 1时候,会产生overflow问题,程序进入死循环。
4/
0 0
- Java Notes: Some Tricky Problems in Java
- Some notes for Java
- Some tricky problems related to "protected"
- Tricky Problems
- split "|" notes in java
- Some essential problems in C
- Some confusing mechanisms in Java
- some tips in java coding
- Java Programming tricky Skills
- thinking in java 3 notes
- Java Concurrency In Practice Notes
- thinking in java ch2 notes
- some problems in video quality detect
- some problems
- some problems
- Some work notes in 2012 Q1
- Notes Of Thinking In Java (1)
- this key -- think in java notes
- 同步锁前传 相关术语索引
- 数据管理基本操作-查找
- 邮件协议SMTP/POP3/IMAP
- 树的表示、推出二叉树、特殊二叉树和二叉树的几个重要性质
- Leetcode 113. Path Sum II
- Java Notes: Some Tricky Problems in Java
- Java Notes: Loops and Arrays & Methods and Class
- 历史前进的步伐,隆隆地回回响在我的耳边,令人难以忘怀
- MFC将任意文件放入资源中调用
- 资源加载操作_FindResource_LoadResource_LockResource
- 应用层 加载驱动 和 卸载驱动代码
- Fix for Debug Assertion Failed! Expression: __acrt_first_block == header
- 【Video】音视频领域知识
- Codeforces 699 C. Vacations ( 贪心