public class TestShuzhu { 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){ nums[i]=max; }else if (nums[i]<min){ nums[i]=min; } sum+=nums[i];} avg/=sum/nums.length; System.out.println("数组的最大值是"+max); System.out.println("书阻塞最小值是"+min); System.out.println("平均值是"+avg); } } 打印为什是 数组的最大值是61 书阻塞最小值是61 平均值是0.0 求解
public class TestShuzhu {
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]; //这里应该交换位子,意思是将nums[i](最大值)的值赋给max
}else if (nums[i]<min){
min=nums[i]; //同理
}
sum=sum+nums[i];} //总和
avg=sum/nums.length;
System.out.println("数组的最大值是"+max);
System.out.println("书阻塞最小值是"+min);
System.out.println("平均值是"+avg);
}
}
明白了赋值反了