public class Array {
public static void main(String[] args) {
int[] nums=new int[]{48,28,37,19,69,200,2,463,322,10};
int max = nums[0];
int min = nums[0];
double sum = 0;
double agv = 0;
for(int i = 0;i<=nums.length;i++){
if(nums[i]>max){
max=nums[i];
}
if(nums[i]<min){
min=nums[i];
}
sum = sum +nums[i];
}
agv = sum/nums.length;
System.out.println("输出数组最大值:"+max);
System.out.println("输出数组最小值:"+min);
System.out.println("输出数组平均数:"+agv);
}
}
错误提示:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Demo02.main(Demo02.java:10)
for(int i = 0;i<=nums.length;i++){
if(nums[i]>max){
max=nums[i];
}数组下标越界了,i<nums.length不能等于,因为下标从0开始,长度的计数从1开始,加油,共同进步!