1.找奇数
public class OddTest {
public static boolean isOdd(int i){
return i % 2 == 1;
}
public static void main(String[] args) {
for(int i = -10; i <= 10; i++) {
System.out.println(isOdd(i));
}
}
*将 return i % 2 == 1; 改为return i % 2 != 0;
2.浮点数相减
import java.math.BigDecimal;
public class DoubleMinus {
public static void main(String[] args) {
System.out.printf( 2.0-1.1);
}
}
*将System.out.printf( 2.0-1.1);改为
System.out.println(new BigDecimal(“2.0”).subtract(new BigDecimal(“1.1”))); 或者System.out.printf(“%.1f”, 2.0-1.1);
3.长整除
public class LongDivision {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; //微秒
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; //毫秒
System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);
}
}
* final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; //微秒
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; //毫秒
改为 final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000; //微秒
final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000; //毫秒
(整形数越界了!)
4.数值交换
public class Swap {
public static void main(String[] args) {
int x = 1984;
int y = 2001;
x^= y^= x^= y;
System.out.println(“x= ” + x + “; y= ” + y);
}
}
*将 x^= y^= x^= y;改为y=(x^= (y^= x))^ y;
5.字符串和字符
public class CharAndString {
public static void main(String[] args) {
System.out.println(“H” + “a”);
System.out.println(‘H’ + ‘a’);
}
}
结果为Ha
169
6.字符数组
public class CharArray {
public static void main(String[] args) {
String letters = “ABC”;
char[] numbers = {‘1’, ‘2’, ‘3’};
System.out.print(letters + ” easy as “); 结果为 “ABC easy as” +数组的索引
System.out.print(numbers);//结果为 123
}
}
7.Unicode编码
public class UnicodeTest {
public static void main(String[] args) {
// u0022 是双引号的 Unicode转义字符
System.out.println(“au0022.length() +u0022b”.length());
System.out.println(“a”.length() +”b”.length()); //两条语句等价
}
}
8.打印输出类名
package node;
public class MyClass {
public static void main(String[] args) {
System.out.println(
MyClass.class.getName().
replaceAll(“.”,”/”) + “.class”);
}
}
结果为////////.MyClass
应该改为System.out.println(
MyClass.class.getName().
replaceAll(“\.”,”/”) + “.class”);
9.随机数
import java.util.Random;
public class RandomTest {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch(rnd.nextInt(3)) {
case 1: word = new StringBuffer(‘P’);
case 2: word = new StringBuffer(‘G’);
default: word = new StringBuffer(‘M’);
}
word.append(‘a’);
word.append(‘i’);
word.append(‘n’);
System.out.println(word);
}
}
结果为 ain
*改为switch(rnd.nextInt(3)) {
case 1: word = new StringBuffer(“P”);break;
case 2: word = new StringBuffer(“G”);break;
default: word = new StringBuffer(“M”);
}
10.无情的增量操作
public class ForTest{
public static void main(String[] args){
int j=0;
for(int i=0;i<100;i++){
j=j++;
}
System.out.println(j);
}
}
结果为 0
应改为 j = ++j;
11.整数边界问题
public class whileTest{
public static final int END = Integer.MAX_VALUE;
public static final int START= END -100;
public static void main(String [] args){
int count = 0;
for(int i=START; i<=END; i++){
count++;
System,out.println(count);
}
}
}
* i 到达最大整数时再加1就变成负数了,故陷入死循环,改为 for(int i=START; i< END; i++)
12.计数器的问题
public class Clock{
public static void main(String argvs[]){
int minutes = 0;
for(int ms =0; ms<60*60*1000;ms++){
if(ms % 60*1000 == 0){
minutes++;
System.out.println(minutes);
}
}
}
}
改为 if(ms % (60*1000) == 0)
13.优柔寡断的返回值
public static boolean decision(){
try{
return true;
}finally{
return false;
}
}
结果为 false ,注意不要在try块中返回值
14
public static void main(String [] args){
try{
System.out.println(“hello”);
System.exit(0);
}finally{
System.out.print(“Good bye!”);
}
}
结果为 hello
15.IO操作
try{
in = new FileInputStrem(src):
out = new FileOutputStrem(dest):
…
}finally{
if(in != null) in.close();//应该改为 if(in != null) {try{in.close();}finally{ in = null;}}
if(out != null) out.close();//同理
}