1 判断奇偶数(***)
/*
*
判断奇偶数
*/
public
class
No1 {
//
正确写法
public
static
boolean
isOdd_right(
int
num){
return
num%2!=0
;
}
//
错误写法
public
static
boolean
isOdd_wrong(
int
num){
return
num%2==1;
}
/*
*
错误分析:只考虑到正整数的情况,没有考虑到负数的情况
*/
}
2 小数相减(*****)
/**
*
*
小数相减
*/
public
class
No2 {
//
正确写法
(
结果:
0.9)
public
static
void
minus_right(){
System.
out
.println(
new
BigDecimal(
"2.0"
).subtract(
new
BigDecimal(
"1.1"
)));
}
//
错误写法
(
结果:
0.8999999999999999)
public
static
void
minus_wrong(){
System.
out
.println(2.0-1.1);
}
}
3 数字越界(*****)
/**
*
数字越界
*/
public
class
No3 {
//
正确写法
(
答案:
1000)
public
static
void
overBorder_right(){
long
a=24
L
*60*60*1000*1000;
long
b=24
L
*60*60*1000;
System.
out
.println(a/b);
}
//
错误写法
(
答案:
5)
public
static
void
overBorder_wrong(){
long
a=24*60*60*1000*1000;
long
b=24*60*60*1000;
System.
out
.println(a/b);
}
/**
*
产生原因:
java
在计算乘法时,乘号两遍按照
int
类型相乘,最后
int
越界。
*/
}
4 互换内容(**)
/**
*
互换内容
*/
public
class
No4 {
//
正确写法
(
答案:
x=2001;y=1984)
public
static
void
swap_right(){
int
x=1984;
int
y=2001;
y=(x^=(y^=x))^y;
System.
out
.println(
"x="
+x+
";y="
+y);
}
//
错误写法
(
答案:
x=0;y=1984)
public
static
void
swap_wrong(){
int
x=1984;
int
y=2001;
x^=y^=x^=y;
System.
out
.println(
"x="
+x+
";y="
+y);
}
/**
*
产生原因:
java
运算顺序
*/
}
5 字符串和字符(*****)
/**
*
字符串和字符
*/
public
class
No5 {
//
注意写法
(
答案:
Ha 169)
public
static
void
charAndString_right(){
System.
out
.println(
"H"
+
"a"
);
System.
out
.println(
'H'
+
'a'
);
}
/**
*
产生原因:
char
相加会转化成
int
相加
*/
}
6 字符数组(****)
/**
*
字符数组
*/
public
class
No6 {
//
注意写法
(
答案:
ABC easy as [C@53606bf5)
public
static
void
charArray_right(){
String letters=
"ABC"
;
char
[] numbers={
'1'
,
'2'
,
'3'
};
System.
out
.println(numbers);
//
答案:
123
System.
out
.println(letters +
" easy as "
+ numbers);
}
/**
*
产生原因:字符数组会自动
toString
。
*/
}
7 转义字符(**)
/**
*
转义字符
*/
public
class
No7 {
//
注意写法
(
答案:
2)
public
static
void
unicodeTest(){
System.
out
.println(
"a\u0022.length() +\u0022b"
.length());
}
/**
*
产生原因:
"\0022"
代表了双引号。相当于打印
"a".length()+"b".length()
*/
}
8 打印输出类名(**)
/**
*
打印输出类名
*/
public
class
No8 {
//
正确写法
(
答案:
javaApplication/No8.class)
public
static
void
outClassName_right(){
System.
out
.println(No8.
class
.getName().replaceAll(
"
\\
."
,
"/"
)+
".class"
);
}
//
错误写法
(
答案:
///////////////////.class)
public
static
void
outClassName_wrong(){
System.
out
.println(No8.
class
.getName().replaceAll(
"."
,
"/"
)+
".class"
);
}
/**
*
产生原因:
"."
需要添加转译字符
*/
}
9 随机数问题(**)
/**
*
随机数问题
*/
public
class
No9 {
private
static
Random
random
=
new
Random();
//
正确写法
(
答案:
Pain
或
Gain
或
Main)
public
static
void
random_right(){
StringBuffer word=
null
;
switch
(
random
.nextInt(
3
)){
case
1:word=
new
StringBuffer(
"
P
"
);
break;
case
2:word=
new
StringBuffer(
"
G
"
);
break;
default
:word=
new
StringBuffer(
"
M
"
);
}
word.append(
'a'
);
word.append(
'i'
);
word.append(
'n'
);
System.
out
.println(word);
}
//
错误写法
(
答案:
ain
)
public
static
void
random_wrong(){
StringBuffer word=
null
;
switch
(
random
.nextInt(2)){
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);
}
/**
*
产生原因:分析:
* 1.nextInt(2)
,只会取到
0,1
两个数值。
case 2
不会执行
* 2.switch
中没有添加
break
。
* 3.new StringBuffer()
时,是用的
char
,会转化成
int
,做为
StringBuffer
的初始长度。
*/
}
10 增量操作问题 (*****)
/**
*
增量问题
*/
public
class
No10 {
//
错误写法
(
答案:
0)
public
static
void
for_wrong(){
int
j=0;
for
(
int
i=0;i<100;i++){
j++;
}
System.
out
.println(j);
}
//
正确写法
(
答案:
100)
public
static
void
for_right(){
int
j=0;
for
(
int
i=0;i<100;i++){
j=++j;
}
System.
out
.println(j);
}
/**
*
产生原因:注意
j++
和
++j
的区别。
j=j++
相当于
j=j
*/
}
11 整数边界问题(****)
/**
*
整数边界问题
*/
public
class
No11 {
public
static
final
int
END
=Integer.
MAX_VALUE
;
public
static
final
int
START
=
END
-100;
//
错误写法
(
答案:死循环,没有结果
)
public
static
void
IntegerBorder_wrong(){
int
count=0;
for
(
int
i=
START
;i<
=
END
;i++){
count++;
}
System.
out
.println(count);
}
//
正确写法
(
答案:
100)
public
static
void
IntegerBorder_right(){
int
count=0;
for
(
int
i=
START
;i<
END
;i++){
count++;
}
System.
out
.println(count);
}
/**
*
产生原因:到达
integer
的边界还要加
1
,造成问题
*/
}
12 计数器问题(****)
/**
*
计数器问题
*/
public
class
No12 {
//
错误写法
(
答案:
60000)
public
static
void
count_wrong(){
int
minutes=0;
for
(
int
ms=0;ms<60*60*1000;ms++){
if
(ms%60*1000==0){
minutes++;
}
}
System.
out
.println(minutes);
}
//
正确写法
(
答案:
60)
public
static
void
count_right(){
int
minutes=0;
for
(
int
ms=0;ms<60*60*1000;ms++){
if
(ms%
(
60*1000
)
==0){
minutes++;
}
}
System.
out
.println(minutes);
}
/**
*
产生原因:运算符的优先级问题
*/
}
13 返回值问题(****)
/**
*
返回值问题
*/
public
class
No13 {
//
注意写法
(
答案:
false)
public
static
boolean
returnVal_wrong(){
try
{
return
true
;
}
finally
{
return
false
;
}
}
/**
*
产生原因:
finally
是一定要执行的。捕获中
try
中的
return
,最终执行了
finally
*/
}
14 系统终止问题(****)
/**
*
系统终止问题
*/
public
class
No14 {
//
注意写法
(
答案:
Hello)
public
static
void
systemExit(){
try
{
System.
out
.println(
"Hello"
);
System.exit(0);
}
finally
{
System.
out
.println(
"goodBye"
);
}
}
/**
*
产生原因:程序终止了
*/
}
15 是否关闭问题(****)
/**
*
是否关闭问题
*/
public
class
No15 {
//
错误写法
public
static
void
isClose_wrong()
throws
IOException{
InputStream in=
null
;
OutputStream out=
null
;
try
{
in=
new
FileInputStream(
"1.txt"
);
out=
new
FileOutputStream(
"2.txt"
);
byte
[] buf=
new
byte
[1024];
int
n;
while
((n=in.read(buf))>0){
out.write(buf,0,n);
}
}
finally
{
if
(in!=
null
){
in.close();
}
if
(out!=
null
){
out.close();
}
}
}
//
正确写法
public
static
void
isClose_right()
throws
IOException{
InputStream in=
null
;
OutputStream out=
null
;
try
{
in=
new
FileInputStream(
"1.txt"
);
out=
new
FileOutputStream(
"2.txt"
);
byte
[] buf=
new
byte
[1024];
int
n;
while
((n=in.read(buf))>0){
out.write(buf,0,n);
}
}
finally
{
if
(in!=
null
){
try
{
in.close();
}
finally
{
in=
null
;
}
}
if
(out!=
null
){
try
{
out.close();
}
finally
{
out=
null
;
}
}
}
}
/**
*
产生原因:
close()
方法也有可能抛出异常
*/
}