java案例统计区域防守漏洞出现几次?

wen java案例 1

本文目录导读:

java案例统计区域防守漏洞出现几次?

  1. 基础数据模型
  2. 统计数据服务类
  3. 主程序和测试案例
  4. 输出结果示例
  5. 使用提示

我来为你提供一个Java案例,用于统计区域防守漏洞出现的次数,这里我会设计一个简单的数据模型和统计逻辑。

基础数据模型

import java.util.*;
import java.util.stream.Collectors;
// 防守区域枚举
enum DefenseZone {
    ZONE_A("禁区左下"),
    ZONE_B("禁区中路"),
    ZONE_C("禁区右上"),
    ZONE_D("中场"),
    ZONE_E("后场");
    private String description;
    DefenseZone(String description) {
        this.description = description;
    }
    public String getDescription() {
        return description;
    }
}
// 漏洞记录类
class DefenseVulnerability {
    private String matchId;        // 比赛ID
    private String teamName;        // 球队名称
    private DefenseZone zone;       // 防守区域
    private String vulnerabilityType; // 漏洞类型
    private int minute;             // 比赛时间(分钟)
    private Date date;              // 日期
    // 构造函数
    public DefenseVulnerability(String matchId, String teamName, 
                               DefenseZone zone, String vulnerabilityType, 
                               int minute, Date date) {
        this.matchId = matchId;
        this.teamName = teamName;
        this.zone = zone;
        this.vulnerabilityType = vulnerabilityType;
        this.minute = minute;
        this.date = date;
    }
    // Getters
    public String getMatchId() { return matchId; }
    public String getTeamName() { return teamName; }
    public DefenseZone getZone() { return zone; }
    public String getVulnerabilityType() { return vulnerabilityType; }
    public int getMinute() { return minute; }
    public Date getDate() { return date; }
    @Override
    public String toString() {
        return String.format("比赛:%s 球队:%s 区域:%s 类型:%s 时间:%d分钟", 
                            matchId, teamName, zone.getDescription(), vulnerabilityType, minute);
    }
}

统计数据服务类

class DefenseStatisticsService {
    private List<DefenseVulnerability> vulnerabilities;
    public DefenseStatisticsService() {
        this.vulnerabilities = new ArrayList<>();
    }
    // 添加漏洞记录
    public void addVulnerability(DefenseVulnerability vulnerability) {
        vulnerabilities.add(vulnerability);
    }
    // 按区域统计漏洞次数
    public Map<DefenseZone, Long> countByZone() {
        return vulnerabilities.stream()
                .collect(Collectors.groupingBy(DefenseVulnerability::getZone, 
                                               Collectors.counting()));
    }
    // 按漏洞类型统计
    public Map<String, Long> countByType() {
        return vulnerabilities.stream()
                .collect(Collectors.groupingBy(DefenseVulnerability::getVulnerabilityType, 
                                               Collectors.counting()));
    }
    // 按球队统计
    public Map<String, Long> countByTeam() {
        return vulnerabilities.stream()
                .collect(Collectors.groupingBy(DefenseVulnerability::getTeamName, 
                                               Collectors.counting()));
    }
    // 按时间区间统计(上半场/下半场)
    public Map<String, Long> countByHalf() {
        Map<String, Long> stats = new HashMap<>();
        long firstHalf = vulnerabilities.stream()
                .filter(v -> v.getMinute() <= 45)
                .count();
        long secondHalf = vulnerabilities.stream()
                .filter(v -> v.getMinute() > 45)
                .count();
        stats.put("上半场(0-45分钟)", firstHalf);
        stats.put("下半场(46-90分钟)", secondHalf);
        return stats;
    }
    // 按区域和漏洞类型组合统计
    public Map<String, Long> countByZoneAndType() {
        return vulnerabilities.stream()
                .collect(Collectors.groupingBy(
                        v -> v.getZone().name() + "_" + v.getVulnerabilityType(),
                        Collectors.counting()));
    }
    // 获取特定区域的漏洞
    public List<DefenseVulnerability> getVulnerabilitiesByZone(DefenseZone zone) {
        return vulnerabilities.stream()
                .filter(v -> v.getZone() == zone)
                .collect(Collectors.toList());
    }
    // 获取漏洞最多的区域
    public DefenseZone getMostVulnerableZone() {
        Map<DefenseZone, Long> zoneCounts = countByZone();
        return zoneCounts.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
    }
    // 获取漏洞最多的球队
    public String getMostVulnerableTeam() {
        Map<String, Long> teamCounts = countByTeam();
        return teamCounts.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
    }
    // 获取所有统计汇总
    public String getFullStatistics() {
        StringBuilder sb = new StringBuilder();
        sb.append("========== 区域防守漏洞统计报告 ==========\n\n");
        sb.append("1. 按区域统计:\n");
        countByZone().forEach((zone, count) -> 
            sb.append(String.format("   - %s: %d 次\n", zone.getDescription(), count)));
        sb.append("\n2. 按漏洞类型统计:\n");
        countByType().forEach((type, count) -> 
            sb.append(String.format("   - %s: %d 次\n", type, count)));
        sb.append("\n3. 按球队统计:\n");
        countByTeam().forEach((team, count) -> 
            sb.append(String.format("   - %s: %d 次\n", team, count)));
        sb.append("\n4. 按时间统计:\n");
        countByHalf().forEach((half, count) -> 
            sb.append(String.format("   - %s: %d 次\n", half, count)));
        sb.append("\n5. 组合统计(区域+类型):\n");
        countByZoneAndType().forEach((key, count) -> {
            String[] parts = key.split("_");
            DefenseZone zone = DefenseZone.valueOf(parts[0]);
            sb.append(String.format("   - %s_%s: %d 次\n", 
                                  zone.getDescription(), parts[1], count));
        });
        return sb.toString();
    }
}

主程序和测试案例

public class DefenseVulnerabilityAnalyzer {
    public static void main(String[] args) {
        // 创建统计服务
        DefenseStatisticsService service = new DefenseStatisticsService();
        // 模拟数据 - 生成一些测试数据
        Random random = new Random(42); // 固定随机种子便于复现
        String[] teams = {"皇家马德里", "巴塞罗那", "拜仁慕尼黑", "利物浦", "曼城"};
        String[] vulnerabilityTypes = {"漏人", "位置失误", "盯人不紧", "传球失误", "拦截失败"};
        DefenseZone[] zones = DefenseZone.values();
        // 生成100条模拟数据
        for (int i = 0; i < 100; i++) {
            String matchId = "MATCH-" + (1000 + i);
            String team = teams[random.nextInt(teams.length)];
            DefenseZone zone = zones[random.nextInt(zones.length)];
            String vType = vulnerabilityTypes[random.nextInt(vulnerabilityTypes.length)];
            int minute = random.nextInt(90) + 1;
            Date date = new Date(2024, random.nextInt(12), random.nextInt(28));
            DefenseVulnerability vul = new DefenseVulnerability(
                matchId, team, zone, vType, minute, date);
            service.addVulnerability(vul);
        }
        // 输出完整统计报告
        System.out.println(service.getFullStatistics());
        // 特定分析
        System.out.println("\n========== 重点分析 ==========\n");
        // 漏洞最多的区域
        DefenseZone mostZone = service.getMostVulnerableZone();
        System.out.println("漏洞最多的区域: " + 
            (mostZone != null ? mostZone.getDescription() : "无数据"));
        // 漏洞最多的球队
        String mostTeam = service.getMostVulnerableTeam();
        System.out.println("漏洞最多的球队: " + 
            (mostTeam != null ? mostTeam : "无数据"));
        // 特定区域的漏洞详情
        System.out.println("\n禁区中路漏洞详情:");
        service.getVulnerabilitiesByZone(DefenseZone.ZONE_B)
                .stream()
                .limit(5) // 只显示前5条
                .forEach(System.out::println);
        // 按时间顺序排列漏洞
        System.out.println("\n按比赛时间排序的漏洞记录(前10条):");
        service.countByHalf().forEach((half, count) -> 
            System.out.println(half + ": " + count + " 次"));
    }
}

输出结果示例

运行程序后,输出结果类似这样:

========== 区域防守漏洞统计报告 ==========
1. 按区域统计:
   - 禁区左下: 18 次
   - 禁区中路: 25 次
   - 禁区右上: 20 次
   - 中场: 22 次
   - 后场: 15 次
2. 按漏洞类型统计:
   - 漏人: 22 次
   - 位置失误: 18 次
   - 盯人不紧: 25 次
   - 传球失误: 17 次
   - 拦截失败: 18 次
3. 按球队统计:
   - 皇家马德里: 23 次
   - 巴塞罗那: 19 次
   - 拜仁慕尼黑: 20 次
   - 利物浦: 18 次
   - 曼城: 20 次
4. 按时间统计:
   - 上半场(0-45分钟): 48 次
   - 下半场(46-90分钟): 52 次
5. 组合统计(区域+类型):
   - 禁区左下_漏人: 5 次
   - 禁区左下_位置失误: 4 次
   ...
========== 重点分析 ==========
漏洞最多的区域: 禁区中路
漏洞最多的球队: 皇家马德里
禁区中路漏洞详情:
比赛:MATCH-1001 球队:利物浦 区域:禁区中路 类型:盯人不紧 时间:45分钟
...

使用提示

这个案例实现了:

  • 多维统计:按区域、类型、球队、时间段统计
  • 组合分析:区域+类型的交叉统计
  • 重点分析:找出最薄弱的环节
  • 数据过滤:按特定条件查询漏洞

你可以根据实际需求调整:

  • 修改DefenseZone枚举来适应不同场地分区
  • 增加更多统计维度(如按对手、按比赛阶段)
  • 添加图形化展示(如生成图表)
  • 增加数据库持久化支持

如有特殊需求,可以告诉我,我可以进一步调整代码。

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