请用【java】帮我写一段输入七门课程成绩,最后对成绩进行排序,计算成绩总分,平均分,筛选不及格成绩
时间: 2024-12-02 17:27:53 浏览: 0
当然可以。首先,我们创建一个`Course`类来存储一门课程的信息,然后编写一个主函数来获取用户输入并处理。以下是使用Java编写的示例:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Course {
String name;
int score;
public Course(String name, int score) {
this.name = name;
this.score = score;
}
// getters and setters (省略)
}
public class Main {
public static void main(String[] args) {
ArrayList<Course> courses = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入七门课程名称及成绩(格式:课程名 分数),例如:数学 90");
for (int i = 0; i < 7; i++) {
String input = scanner.nextLine();
String[] parts = input.split(" ");
if (parts.length == 2) {
courses.add(new Course(parts[0], Integer.parseInt(parts[1])));
} else {
System.out.println("输入格式错误,请重新输入第" + (i + 1) + "门课的成绩。");
i--; // 重试当前课程
}
}
// 对成绩进行排序
Collections.sort(courses, (a, b) -> a.score - b.score);
// 计算总分和平均分
int totalScore = 0;
for (Course course : courses) {
totalScore += course.score;
}
double averageScore = (double) totalScore / courses.size();
// 筛选不及格成绩
System.out.println("\n不及格的成绩:");
for (Course course : courses) {
if (course.score < 60) {
System.out.println(course.name + ": " + course.score);
}
}
System.out.printf("总分: %d%n", totalScore);
System.out.printf("平均分: %.2f%n", averageScore);
}
}
```
阅读全文