Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at dream.geshu.main(geshu.java:15)为什么会出现这样的错误?
ackage dream;
public class geshu {
/**
* 求奇数个数和偶数个数
*/
public static void main(String[] args) {
int sum=0;
int c=0;
int a[]=new int[]{1,2,3,4,5,6,7,8};
boolean bool=false;
for(int i=0;i<9;i++)
{
if(a[i]%2==0)
{
sum=sum+1;
bool=true;
}
else
{
c=c+1;
}
}
if(bool)
{
System.out.print("奇数的个数:"+sum);
}
else
{
System.out.print("偶数的个数:"+c);
}
}
}