public class lianxi2 {
public static void main(String[] args) {
int[] nums=new int[]{61,23,4,74,13,148,20};
int max=nums[0];
int min=nums[0];
double sum=0;
double avg=0;
for(int i=0;i<nums.length;i++){
if(nums[i]>max){
max=nums[i];
}
else if(nums[i]<min){
min=nums[i];
}
sum=sum+nums[i];
}
avg=sum/(i+1);
System.out.println("数组中最大值:"+max);
System.out.println("数组中最小值:"+min);
System.out.println("数组中平均值为"+avg);
}
}
i是for循环的局部变量,你的avg=sum/(i+1) 代码是在for之外的,代表什么呢?所以把分母改成nums.length