Java 输入 10个学生的英语成绩,统计并输出该门课程的平均分以及不及格学生的人数,平均分保留1位小数。 输出两行,第一行为平均分,第二行为不及格学生的人数。
时间: 2024-10-17 19:13:41 浏览: 62
在Java中,你可以通过Scanner类获取用户输入,创建一个ArrayList存储学生的成绩,并计算平均分和不及格人数。这里是一个简单的示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> scores = new ArrayList<>();
int totalStudents = 10;
int passingScore = 60; // 可以设置及格线为60分
System.out.println("请输入10个学生的英语成绩:");
for (int i = 0; i < totalStudents; i++) {
int score = scanner.nextInt();
scores.add(score);
}
double averageScore = calculateAverage(scores);
int failedStudentsCount = countFailedScores(scores, passingScore);
System.out.printf("平均分: %.1f%n", averageScore);
System.out.println("不及格学生人数: " + failedStudentsCount);
scanner.close();
}
private static double calculateAverage(ArrayList<Integer> scores) {
double sum = 0;
for (int score : scores) {
sum += score;
}
return sum / scores.size();
}
private static int countFailedScores(ArrayList<Integer> scores, int passingScore) {
int count = 0;
for (int score : scores) {
if (score < passingScore) {
count++;
}
}
return count;
}
}
```
这个程序首先提示用户输入10个分数,然后计算它们的平均值和不及格的学生人数。注意,`calculateAverage`方法用于计算平均分,`countFailedScores`方法则检查哪些分数低于及格线。最后将结果输出到控制台。
阅读全文
相关推荐

















