class try0521{
public static void main(String[] args){
String score="75,88,62,77,69,82,79,85,93,4l,81,53,66,71";
String[] token=score.split("[,]");
try{
int num=0, sum=0;
int[] data=new int[token.length];
for(int i=1;i<=token.length;i++)
data[i]=Integer.parseInt(token[i]);
for(int dd:data) sum=sum+dd;
System.out.println(" Average : "+sum/num);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println(" ArrayIndexOutOfBoundsException: "+e);
}catch(NumberFormatException e){
System.out.println("NumberFormatException: "+e);
}catch(ArithmeticException e){
System.out.println("ArithmeticException: "+e);
}
}
}第一點
出現以下訊息
NumberFormatException: java.lang.NumberFormatException: For input string: "4l"
String score="75,88,62,77,69,82,79,85,93,41,81,53,66,71"; 將4l改正為41之後
第二點
出現以下訊息
ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: 14
for(int i=1;i<token.length;i++)改為 i < token.length
第三點
出現以下訊息
ArithmeticException: java.lang.ArithmeticException: / by zero
加上num=token.length;
以上三點例外處理改正之後就為正常運行
沒有留言:
張貼留言