某班30名学生,试用一维数组存储班级学生英语成绩,并利用函数统计平均分。要求:利用Math类中的Random()方法随机生成英语成绩,成绩范围(0~100)
时间: 2024-05-29 12:10:06 浏览: 78
import java.util.Arrays;
import java.util.Random;
public class EnglishScore {
public static void main(String[] args) {
int[] scores = new int[30];
Random random = new Random();
for (int i = 0; i < scores.length; i++) {
scores[i] = random.nextInt(101);
}
System.out.println(Arrays.toString(scores));
double average = calculateAverage(scores);
System.out.println("平均分:" + average);
}
public static double calculateAverage(int[] scores) {
double sum = 0;
for (int score : scores) {
sum += score;
}
return sum / scores.length;
}
}
阅读全文