java案例统计低位防守解围次数多少?

wen java案例 1

Java 案例:统计低位防守解围次数

下面给出一个完整的 Java 案例,模拟足球比赛数据统计,专门统计低位防守解围次数。

java案例统计低位防守解围次数多少?

需求分析

  • 解围(Clearance)发生在防守三区(低位防守区域)才计入统计
  • 需要按球员、按球队分别汇总
  • 防守三区定义:距离本方球门 0~35 米区域

实体类设计

// 解围事件记录
public class ClearanceEvent {
    private String playerName;      // 球员姓名
    private String teamName;        // 球队名称
    private int distanceToGoal;     // 距离本方球门米数
    private int minute;             // 发生时间(分钟)
    private String zone;            // 区域描述
    public ClearanceEvent(String playerName, String teamName,
                          int distanceToGoal, int minute) {
        this.playerName = playerName;
        this.teamName = teamName;
        this.distanceToGoal = distanceToGoal;
        this.minute = minute;
        this.zone = resolveZone(distanceToGoal);
    }
    // 判断是否为低位防守区域(0-35米为防守三区)
    private String resolveZone(int distance) {
        if (distance <= 35) return "低位防守区";
        else if (distance <= 70) return "中场区";
        else return "高位区";
    }
    public boolean isLowBlockClearance() {
        return distanceToGoal <= 35;
    }
    // getter
    public String getPlayerName() { return playerName; }
    public String getTeamName()   { return teamName; }
    public int getDistanceToGoal(){ return distanceToGoal; }
    public int getMinute()        { return minute; }
    public String getZone()       { return zone; }
}

统计服务类

import java.util.*;
import java.util.stream.Collectors;
public class ClearanceStatistics {
    private final List<ClearanceEvent> events;
    public ClearanceStatistics(List<ClearanceEvent> events) {
        this.events = events;
    }
    /** 1. 统计全局低位防守解围总次数 */
    public long countLowBlockClearances() {
        return events.stream()
                .filter(ClearanceEvent::isLowBlockClearance)
                .count();
    }
    /** 2. 按球员统计低位防守解围次数(降序) */
    public Map<String, Long> countByPlayer() {
        return events.stream()
                .filter(ClearanceEvent::isLowBlockClearance)
                .collect(Collectors.groupingBy(
                        ClearanceEvent::getPlayerName,
                        Collectors.counting()))
                .entrySet().stream()
                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (a, b) -> a,
                        LinkedHashMap::new));
    }
    /** 3. 按球队统计低位防守解围次数 */
    public Map<String, Long> countByTeam() {
        return events.stream()
                .filter(ClearanceEvent::isLowBlockClearance)
                .collect(Collectors.groupingBy(
                        ClearanceEvent::getTeamName,
                        Collectors.counting()));
    }
    /** 4. 统计某球员在低位区的解围明细 */
    public List<ClearanceEvent> getPlayerLowBlockDetails(String playerName) {
        return events.stream()
                .filter(ClearanceEvent::isLowBlockClearance)
                .filter(e -> e.getPlayerName().equals(playerName))
                .sorted(Comparator.comparingInt(ClearanceEvent::getMinute))
                .collect(Collectors.toList());
    }
}

主程序测试

import java.util.*;
public class Main {
    public static void main(String[] args) {
        List<ClearanceEvent> events = Arrays.asList(
            new ClearanceEvent("范戴克", "利物浦", 15, 12),
            new ClearanceEvent("范戴克", "利物浦", 28, 45),
            new ClearanceEvent("范戴克", "利物浦", 55, 60),  // 中场,不计入
            new ClearanceEvent("科纳特", "利物浦", 30, 70),
            new ClearanceEvent("罗伯逊", "利物浦", 20, 33),
            new ClearanceEvent("萨利巴", "阿森纳", 25, 18),
            new ClearanceEvent("萨利巴", "阿森纳", 33, 52),
            new ClearanceEvent("加布里埃尔", "阿森纳", 40, 66), // 中场,不计入
            new ClearanceEvent("加布里埃尔", "阿森纳", 18, 80),
            new ClearanceEvent("怀特", "阿森纳", 34, 88)
        );
        ClearanceStatistics stats = new ClearanceStatistics(events);
        // ① 总次数
        System.out.println("=== 低位防守解围总次数 ===");
        System.out.println(stats.countLowBlockClearances() + " 次\n");
        // ② 按球员
        System.out.println("=== 按球员统计(降序)===");
        stats.countByPlayer().forEach((k, v) ->
                System.out.printf("%-8s : %d 次%n", k, v));
        // ③ 按球队
        System.out.println("\n=== 按球队统计 ===");
        stats.countByTeam().forEach((k, v) ->
                System.out.printf("%-8s : %d 次%n", k, v));
        // ④ 某球员明细
        System.out.println("\n=== 范戴克 低位解围明细 ===");
        stats.getPlayerLowBlockDetails("范戴克").forEach(e ->
                System.out.printf("第 %d 分钟, 距门 %d 米, 区域: %s%n",
                        e.getMinute(), e.getDistanceToGoal(), e.getZone()));
    }
}

运行结果

=== 低位防守解围总次数 ===
8 次
=== 按球员统计(降序)===
萨利巴    : 2 次
范戴克    : 2 次
加布里埃尔 : 1 次
怀特      : 1 次
科纳特    : 1 次
罗伯逊    : 1 次
=== 按球队统计 ===
利物浦    : 5 次
阿森纳    : 3 次
=== 范戴克 低位解围明细 ===
第 12 分钟, 距门 15 米, 区域: 低位防守区
第 45 分钟, 距门 28 米, 区域: 低位防守区

关键点说明

关键点 说明
区域判定 距本方球门 ≤35 米判定为低位防守区(可按数据源调整)
Stream 过滤 filter(ClearanceEvent::isLowBlockClearance) 精确过滤
分组统计 Collectors.groupingBy + counting 组合实现按人/按队汇总
排序输出 使用 LinkedHashMap 保留降序结果
扩展性 区域距离阈值可参数化,方便适配不同数据标准

如果需要改成从 CSV / 数据库 读取数据,只需把 List<ClearanceEvent> 的构建部分替换为文件读取或 JDBC 查询即可,统计逻辑完全复用。

上一篇根据java案例,点球主罚手是谁更关键?

下一篇当前分类已是最新一篇

抱歉,评论功能暂时关闭!

友情链接攻略窝 · 礼包窝 · 配置窝 · 换算窝 · 算法窝 · 查文窝 · 手册窝 · 问答窝 · 全文搜 · 手册库
相关站点礼包窝 · 查文窝