}//抛出ArrayIndexOutOfBoundsException
int len = str.length();
while(len>=3){
len = len - 3;
str.insert(len,",");
}
int i = str.length();
while (i >= 0) {
if (i >= 3) {
i -= 3;
str.insert(i, ',');
} else {
System.out.println(str);
break;
}
}改下
当while判断的i比3小时在判断语句的else内 : 语句(i-=3)则会出现负值,此时,语句 str.insert(i, ',') 找不到对应得数组下标,系统出现数组越界异常提示。
改正:把第3行的代码 if(i<=0) 改为 if(i<=3)即可解决越界问题。