奥运会跳水比赛通常有7名裁判,打分的方式为去掉一个最高分,去掉一个最低分,剩余5个成绩求得平均值,该平均值乘以3再乘以难度系数为最终成绩,请编程实现7名裁判单次跳水打分的输入,难度系数输入(难度系数通常在2~4之间),并输出有效成绩排序以及最终得分。JAVA
时间: 2024-10-14 09:15:54 浏览: 45
在Java中,你可以通过Scanner类获取用户输入,然后使用ArrayList来存储每个裁判的成绩,并计算去掉最高分和最低分后的平均分。最后,将这个平均分乘以难度系数得到最终得分。这里是一个简单的示例程序:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class DivingScoreCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入7位裁判的评分(范围0-10,每组逗号隔开):");
String scoresStr = scanner.nextLine();
ArrayList<Integer> scores = new ArrayList<>(Arrays.stream(scoresStr.split(",")) // Split by comma
.mapToInt(Integer::parseInt)
.boxed() // Convert to Integer objects
.filter(score -> score >= 0 && score <= 10) // Filter out invalid scores
.collect(Collectors.toList()));
if (scores.size() < 7) {
System.out.println("至少需要7个有效评分,请重新输入.");
return;
}
Collections.sort(scores); // Sort the scores in ascending order
int sumAfterFilter = scores.subList(1, scores.size() - 1).stream().mapToInt(Integer::intValue).sum(); // Sum of middle 5 scores
double average = (double) sumAfterFilter / (scores.size() - 2); // Average of middle 5 scores
double difficultyFactor = scanner.nextDouble(); // Get the difficulty factor (should be between 2 and 4)
if (difficultyFactor < 2 || difficultyFactor > 4) {
System.out.println("难度系数必须在2到4之间,请重新输入.");
return;
}
double finalScore = average * difficultyFactor * 3; // Final score calculation
System.out.println("有效成绩排序: " + scores);
System.out.println("最终得分: " + finalScore);
}
}
```
阅读全文