搭建流水线:程序有时候需要将任务按流水线进行,例如评判比赛选手 的任务按流水线分为依次进行的三个步骤:录入裁判给选手的分数,去掉一个最 高分和一个最低分,计算出平均成绩。编写程序,搭建评判选手流水线。在流水线上再增加一个对象,该对象负责判 断选手的成绩是否达到及格标准。用java语言来实现
时间: 2024-02-01 10:16:14 浏览: 114
裁判打分程序
5星 · 资源好评率100%
以下是一个简单的Java程序,用于搭建评判选手流水线:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Pipeline {
public static void main(String[] args) {
// 创建选手列表
List<Player> players = new ArrayList<>();
players.add(new Player("小明", 80));
players.add(new Player("小红", 90));
players.add(new Player("小刚", 85));
// 创建流水线对象
Pipeline pipeline = new Pipeline();
// 录入分数
pipeline.recordScore(players);
// 去掉最高分和最低分
pipeline.removeExtremeScores(players);
// 计算平均成绩
double averageScore = pipeline.calculateAverageScore(players);
// 判断是否及格
pipeline.checkPass(players, averageScore);
}
// 录入裁判给选手的分数
public void recordScore(List<Player> players) {
for (Player player : players) {
player.setScore((int) (Math.random() * 100));
System.out.println(player.getName() + "的分数为:" + player.getScore());
}
}
// 去掉一个最高分和一个最低分
public void removeExtremeScores(List<Player> players) {
Collections.sort(players, (p1, p2) -> Integer.compare(p1.getScore(), p2.getScore()));
players.remove(0); // 去掉最低分
players.remove(players.size() - 1); // 去掉最高分
for (Player player : players) {
System.out.println(player.getName() + "的中间三个分数为:" + player.getScores());
}
}
// 计算平均成绩
public double calculateAverageScore(List<Player> players) {
double totalScore = 0;
for (Player player : players) {
totalScore += player.getScore();
}
double averageScore = totalScore / players.size();
System.out.println("选手的平均成绩为:" + averageScore);
return averageScore;
}
// 判断是否及格
public void checkPass(List<Player> players, double passScore) {
for (Player player : players) {
if (player.getScore() >= passScore) {
System.out.println(player.getName() + "及格!");
} else {
System.out.println(player.getName() + "不及格!");
}
}
}
}
class Player {
private String name;
private int score;
private List<Integer> scores = new ArrayList<>();
public Player(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
scores.add(score);
}
public List<Integer> getScores() {
return scores;
}
}
```
这个程序使用了Java的Lambda表达式来定义排序规则,可以实现去掉最高分和最低分的操作。同时,为了记录选手的所有分数,我们在Player类中增加了一个scores列表。
阅读全文