本文目录导读:

我来帮你创建一个Java程序来统计转身过人次数,这通常用于篮球或其他运动数据分析。
完整的Java统计程序
import java.util.*;
import java.util.stream.Collectors;
/**
* 统计球员转身过人次数
*/
public class TurnoverStatistics {
// 球员类
static class Player {
private String name;
private int turnOverCount; // 转身过人次数
public Player(String name, int turnOverCount) {
this.name = name;
this.turnOverCount = turnOverCount;
}
public String getName() {
return name;
}
public int getTurnOverCount() {
return turnOverCount;
}
@Override
public String toString() {
return String.format("%s: %d次", name, turnOverCount);
}
}
// 统计结果类
static class StatisticsResult {
private Player topPlayer;
private List<Player> sortedPlayers;
private Map<String, Integer> playerMap;
public StatisticsResult(Player topPlayer, List<Player> sortedPlayers, Map<String, Integer> playerMap) {
this.topPlayer = topPlayer;
this.sortedPlayers = sortedPlayers;
this.playerMap = playerMap;
}
public Player getTopPlayer() {
return topPlayer;
}
public List<Player> getSortedPlayers() {
return sortedPlayers;
}
public Map<String, Integer> getPlayerMap() {
return playerMap;
}
}
/**
* 统计并分析转身过人数据
*/
public static StatisticsResult analyzeTurnovers(List<Player> players) {
if (players == null || players.isEmpty()) {
throw new IllegalArgumentException("球员列表不能为空");
}
// 转换为Map便于查询
Map<String, Integer> playerMap = players.stream()
.collect(Collectors.toMap(Player::getName, Player::getTurnOverCount));
// 按次数排序(降序)
List<Player> sortedPlayers = players.stream()
.sorted((p1, p2) -> Integer.compare(p2.getTurnOverCount(), p1.getTurnOverCount()))
.collect(Collectors.toList());
// 找出最多的球员
Player topPlayer = sortedPlayers.get(0);
return new StatisticsResult(topPlayer, sortedPlayers, playerMap);
}
/**
* 从文件或外部数据源读取数据(示例方法)
*/
public static List<Player> loadDataFromSource() {
// 这里模拟从数据源读取数据
// 实际项目中可以从数据库、文件或API获取
List<Player> players = new ArrayList<>();
// 示例数据
players.add(new Player("詹姆斯", 25));
players.add(new Player("库里", 32));
players.add(new Player("杜兰特", 18));
players.add(new Player("字母哥", 41));
players.add(new Player("威少", 27));
players.add(new Player("哈登", 35));
players.add(new Player("东契奇", 44));
players.add(new Player("塔图姆", 22));
return players;
}
/**
* 交互式输入数据
*/
public static List<Player> inputPlayerData() {
Scanner scanner = new Scanner(System.in);
List<Player> players = new ArrayList<>();
System.out.println("=== 请输入球员转身过人数据 ===");
System.out.print("请输入球员数量: ");
try {
int count = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < count; i++) {
System.out.println("\n第 " + (i + 1) + " 名球员:");
System.out.print("球员姓名: ");
String name = scanner.nextLine();
System.out.print("转身过人次数: ");
int turnoverCount = Integer.parseInt(scanner.nextLine());
players.add(new Player(name, turnoverCount));
}
} catch (NumberFormatException e) {
System.out.println("输入格式错误,请重新输入");
}
return players;
}
/**
* 计算平均转身次数
*/
public static double calculateAverage(List<Player> players) {
return players.stream()
.mapToInt(Player::getTurnOverCount)
.average()
.orElse(0.0);
}
/**
* 找出并列第一的球员
*/
public static List<Player> findTiedTopPlayers(List<Player> players) {
if (players.isEmpty()) return Collections.emptyList();
int maxCount = players.stream()
.mapToInt(Player::getTurnOverCount)
.max()
.orElse(0);
return players.stream()
.filter(p -> p.getTurnOverCount() == maxCount)
.collect(Collectors.toList());
}
/**
* 打印统计报告
*/
public static void printReport(StatisticsResult result, double average) {
System.out.println("\n========== 转身过人统计报告 ==========");
System.out.println("\n【全场最佳】");
System.out.println("球员: " + result.getTopPlayer().getName());
System.out.println("转身过人次数: " + result.getTopPlayer().getTurnOverCount() + " 次");
List<Player> tiedPlayers = findTiedTopPlayers(result.getSortedPlayers());
if (tiedPlayers.size() > 1) {
System.out.println("\n【并列第一】");
tiedPlayers.forEach(p -> System.out.println(" " + p.getName() + ": " + p.getTurnOverCount() + " 次"));
}
System.out.println("\n【排名榜】");
int rank = 1;
for (Player player : result.getSortedPlayers()) {
System.out.printf("第 %d 名: %s (%d 次)\n", rank++, player.getName(), player.getTurnOverCount());
}
System.out.printf("\n【平均转身过人次数】: %.2f 次\n", average);
// 相对于平均值的比较
System.out.println("\n【表现评价】");
for (Player player : result.getSortedPlayers()) {
int diff = player.getTurnOverCount() - (int) Math.round(average);
String evaluation;
if (diff > 0) {
evaluation = "高于平均 " + diff + " 次";
} else if (diff < 0) {
evaluation = "低于平均 " + Math.abs(diff) + " 次";
} else {
evaluation = "等于平均水平";
}
System.out.println(player.getName() + ": " + evaluation);
}
System.out.println("\n=====================================");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("选择数据输入方式:");
System.out.println("1 - 使用示例数据");
System.out.println("2 - 手动输入数据");
System.out.print("请选择 (1/2): ");
String choice = scanner.nextLine().trim();
List<Player> players;
if ("2".equals(choice)) {
players = inputPlayerData();
} else {
players = loadDataFromSource();
System.out.println("\n已加载示例数据");
players.forEach(System.out::println);
}
if (players.isEmpty()) {
System.out.println("没有球员数据!");
return;
}
// 进行统计分析
StatisticsResult result = analyzeTurnovers(players);
double average = calculateAverage(players);
// 打印报告
printReport(result, average);
// 额外的查询功能
System.out.print("\n是否需要查询特定球员数据? (y/n): ");
if ("y".equalsIgnoreCase(scanner.nextLine().trim())) {
System.out.print("请输入球员姓名: ");
String queryName = scanner.nextLine().trim();
Integer count = result.getPlayerMap().get(queryName);
if (count != null) {
System.out.printf("%s 的转身过人次数: %d 次\n", queryName, count);
} else {
System.out.println("未找到该球员数据");
}
}
scanner.close();
}
}
应用场景扩展示例
/**
* 更高级的应用场景 - 球队统计系统
*/
public class TeamTurnoverAnalyzer {
// 定义比赛记录类
static class GameRecord {
private String gameDate;
private Map<String, Integer> playerTurnovers;
public GameRecord(String gameDate) {
this.gameDate = gameDate;
this.playerTurnovers = new HashMap<>();
}
public void addPlayerTurnover(String player, int count) {
playerTurnovers.put(player, count);
}
public Map<String, Integer> getPlayerTurnovers() {
return playerTurnovers;
}
public String getGameDate() {
return gameDate;
}
}
/**
* 汇总多场比赛数据
*/
public static Map<String, Integer> aggregateMultipleGames(List<GameRecord> games) {
Map<String, Integer> totalTurnovers = new HashMap<>();
for (GameRecord game : games) {
for (Map.Entry<String, Integer> entry : game.getPlayerTurnovers().entrySet()) {
totalTurnovers.merge(entry.getKey(), entry.getValue(), Integer::sum);
}
}
return totalTurnovers;
}
public static void main(String[] args) {
// 输入格式:球员名字 转身次数(用空格分隔)
Map<String, Integer> playerData = new HashMap<>();
// 示例:解析输入
String[] lines = {
"詹姆斯 25",
"库里 30"
};
for (String line : lines) {
String[] parts = line.split("\\s+");
if (parts.length == 2) {
playerData.put(parts[0], Integer.parseInt(parts[1]));
}
}
// 转换为Player对象列表
List<Player> players = playerData.entrySet().stream()
.map(e -> new Player(e.getKey(), e.getValue()))
.collect(Collectors.toList());
// 进行分析
StatisticsResult result = TurnoverStatistics.analyzeTurnovers(players);
System.out.println("转身过人次数最多的球员: " + result.getTopPlayer());
}
}
使用说明
- 运行程序:选择使用示例数据或手动输入
- 查看结果:程序会显示排名、最佳球员、平均数据等
- 扩展功能:代码包含多个可复用的方法
输出示例
========== 转身过人统计报告 ==========
【全场最佳】
球员: 东契奇
转身过人次数: 44 次
【排名榜】
第 1 名: 东契奇 (44 次)
第 2 名: 字母哥 (41 次)
第 3 名: 哈登 (35 次)
...
【平均转身过人次数】: 30.50 次
这个程序可以灵活扩展,比如添加数据库支持、整合其他运动统计数据等,有什么需要调整的吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播