package green;
public class Array {
public static void main(String[] args) {
int[] a = new int[10];
int max = a[0];
int min = a[0];
int sum = 0;
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.println(a[i]);
if (max < a[i]) {
max = a[i];
}if(min > a[i]) {
min = a[i];
}
sum += a[i];
}
System.out.println("最大值:" + max + "最小值:" + min + "总和:" + sum + "平均值:" + sum / 10.0);
}
}
min初始化为0,随机数始终大于0,所内以0最小
我觉得for循环里面应该加min = a[0]
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.println(a[i]);
min = a[0]; //因为数组创建之后 数组中的元素会被自动赋予一个默认值 int 型的默认值是0
if (max < a[i]) {
max = a[i];
}if(min > a[i]) {
min = a[i];
}
sum += a[i];
}