public class imooc_shuzhu2 {
public static void main(String[] args) {
int[] shuzhu = new int[] { 61, 23, 4, 74, 13, 148, 20 };
int max=shuzhu[0];
int min=shuzhu[0];
double sum=0;
double avg=0;
for(int i=0;i<shuzhu.length;i++){
if(shuzhu[i]>max)
sum=shuzhu[i];
if(shuzhu[i]<min)
min=shuzhu[i];
sum+=shuzhu[i];
}
avg=sum/shuzhu.length;
System.out.println("最小值"+min);
System.out.println("最大值"+max);
System.out.println("平均数"+avg);
}
}
public class Test { public static void main(String[] args) { int[] shuzhu = new int[] { 61, 23, 4, 74, 13, 148, 20 }; int max=shuzhu[0]; int min=shuzhu[0]; int sum=0; double avg=0; for(int i=0;i<shuzhu.length;i++){ if(shuzhu[i]>max) max=shuzhu[i]; if(shuzhu[i]<min) min=shuzhu[i]; sum+=shuzhu[i]; } avg=sum/shuzhu.length; System.out.println("最小值"+min); System.out.println("最大值"+max); System.out.println("平均数"+avg); } }
修改一下圈出的部分看看。
sum=shuzhu[i];改成max=shuzhu[i];