本文目录导读:

我为您提供一个Java案例,用于统计足球比赛中界外球进攻威胁次数,这个案例包含数据模型、分析逻辑和示例测试。
数据模型类
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
// 界外球事件类
class ThrowInEvent {
private LocalTime time; // 发生时间
private int area; // 区域(1-6,代表6个区域)
private String team; // 球队名称
private boolean isDeepThrow; // 是否掷入禁区
private String outcome; // 后续结果:进球、射门、传中、普通
private int attackThreatScore; // 威胁评分(1-10)
public ThrowInEvent(LocalTime time, int area, String team, boolean isDeepThrow, String outcome) {
this.time = time;
this.area = area;
this.team = team;
this.isDeepThrow = isDeepThrow;
this.outcome = outcome;
this.attackThreatScore = calculateThreatScore();
}
// 计算威胁评分
private int calculateThreatScore() {
int score = 0;
// 基础分:区域位置(越靠近球门威胁越大)
switch (area) {
case 1: // 禁区左侧
case 4: // 禁区右侧
score += 3;
break;
case 2: // 禁区中路
case 5: // 中场区域
score += 2;
break;
case 3: // 禁区左路
case 6: // 禁区右路
score += 1;
break;
}
// 界外球加分
if (isDeepThrow) {
score += 2;
}
// 结果加分
switch (outcome) {
case "进球":
score += 5;
break;
case "射门":
score += 3;
break;
case "传中":
score += 2;
break;
}
return Math.min(score, 10);
}
// getters
public LocalTime getTime() { return time; }
public int getArea() { return area; }
public String getTeam() { return team; }
public boolean isDeepThrow() { return isDeepThrow; }
public String getOutcome() { return outcome; }
public int getAttackThreatScore() { return attackThreatScore; }
@Override
public String toString() {
return String.format("时间:%s 区域:%d 球队:%s 深度掷球:%s 结果:%s 威胁值:%d",
time, area, team, isDeepThrow ? "是" : "否", outcome, attackThreatScore);
}
}
// 统计结果类
class ThreatStatistics {
private String team;
private int totalThrowIns;
private int threatInstances;
private int highThreatInstances; // 高威胁(评分>=7)
private int goals; // 形成的进球
private int shots; // 形成的射门
private double threatRate; // 威胁率
public ThreatStatistics(String team) {
this.team = team;
}
// getters and setters
public String getTeam() { return team; }
public int getTotalThrowIns() { return totalThrowIns; }
public void incrementTotalThrowIns() { this.totalThrowIns++; }
public int getThreatInstances() { return threatInstances; }
public void incrementThreatInstances() { this.threatInstances++; }
public int getHighThreatInstances() { return highThreatInstances; }
public void incrementHighThreatInstances() { this.highThreatInstances++; }
public int getGoals() { return goals; }
public void incrementGoals() { this.goals++; }
public int getShots() { return shots; }
public void incrementShots() { this.shots++; }
public double getThreatRate() {
return totalThrowIns > 0 ? (double) threatInstances / totalThrowIns * 100 : 0;
}
}
分析引擎类
import java.util.*;
import java.util.stream.Collectors;
// 界外球威胁分析引擎
class ThrowInAnalyzer {
private List<ThrowInEvent> allEvents;
public ThrowInAnalyzer() {
this.allEvents = new ArrayList<>();
}
// 添加事件
public void addEvent(ThrowInEvent event) {
allEvents.add(event);
}
public void addEvents(List<ThrowInEvent> events) {
allEvents.addAll(events);
}
// 核心统计方法 - 统计各队威胁次数
public Map<String, ThreatStatistics> analyzeThreatByTeam() {
Map<String, ThreatStatistics> statsMap = new HashMap<>();
for (ThrowInEvent event : allEvents) {
String team = event.getTeam();
ThreatStatistics stats = statsMap.getOrDefault(team, new ThreatStatistics(team));
// 基础统计
stats.incrementTotalThrowIns();
// 判断是否威胁进攻
if (event.getAttackThreatScore() >= 3) {
stats.incrementThreatInstances();
// 高威胁(评分>=7)
if (event.getAttackThreatScore() >= 7) {
stats.incrementHighThreatInstances();
}
// 结果统计
if ("进球".equals(event.getOutcome())) {
stats.incrementGoals();
} else if ("射门".equals(event.getOutcome())) {
stats.incrementShots();
}
}
statsMap.put(team, stats);
}
return statsMap;
}
// 按区域分析威胁
public Map<Integer, List<ThrowInEvent>> analyzeByArea() {
return allEvents.stream()
.filter(e -> e.getAttackThreatScore() >= 3)
.collect(Collectors.groupingBy(ThrowInEvent::getArea));
}
// 分析特定球队的威胁
public ThreatStatistics analyzeTeam(String teamName) {
Map<String, ThreatStatistics> stats = analyzeThreatByTeam();
return stats.getOrDefault(teamName, new ThreatStatistics(teamName));
}
// 获取高威胁事件
public List<ThrowInEvent> getHighThreatEvents(int minScore) {
return allEvents.stream()
.filter(e -> e.getAttackThreatScore() >= minScore)
.sorted(Comparator.comparing(ThrowInEvent::getAttackThreatScore).reversed())
.collect(Collectors.toList());
}
// 威胁率比较
public String compareTeams() {
Map<String, ThreatStatistics> stats = analyzeThreatByTeam();
StringBuilder sb = new StringBuilder();
sb.append("\n=== 球队威胁率排名 ===\n");
stats.entrySet().stream()
.sorted((a, b) -> Double.compare(
b.getValue().getThreatRate(),
a.getValue().getThreatRate()))
.forEach(entry -> {
ThreatStatistics s = entry.getValue();
sb.append(String.format("%s: 威胁率%.1f%% (总界外球%d, 威胁次数%d, 高威胁%d, 进球%d, 射门%d)%n",
entry.getKey(), s.getThreatRate(), s.getTotalThrowIns(),
s.getThreatInstances(), s.getHighThreatInstances(),
s.getGoals(), s.getShots()));
});
return sb.toString();
}
// 生成详细报告
public String generateReport() {
StringBuilder report = new StringBuilder();
report.append("========== 界外球威胁分析报告 ==========\n");
report.append(compareTeams());
// 高威胁事件详情
List<ThrowInEvent> highThreat = getHighThreatEvents(7);
report.append("\n=== 高威胁事件详情 (评分≥7) ===\n");
if (highThreat.isEmpty()) {
report.append("无高威胁事件\n");
} else {
for (ThrowInEvent event : highThreat) {
report.append(event.toString()).append("\n");
}
}
return report.toString();
}
}
主测试类
import java.time.LocalTime;
import java.util.*;
public class ThrowInThreatAnalyzerTest {
public static void main(String[] args) {
// 创建分析器
ThrowInAnalyzer analyzer = new ThrowInAnalyzer();
// 模拟比赛数据(示例数据)
List<ThrowInEvent> testData = generateTestData();
// 添加所有事件
analyzer.addEvents(testData);
// 执行分析并输出报告
System.out.println(analyzer.generateReport());
// 测试特定球队分析
System.out.println("\n=== 曼联球队详细分析 ===");
ThreatStatistics unitedStats = analyzer.analyzeTeam("曼联");
System.out.printf("总界外球: %d%n", unitedStats.getTotalThrowIns());
System.out.printf("威胁次数: %d%n", unitedStats.getThreatInstances());
System.out.printf("高威胁次数: %d%n", unitedStats.getHighThreatInstances());
System.out.printf("威胁形成进球: %d%n", unitedStats.getGoals());
System.out.printf("威胁形成射门: %d%n", unitedStats.getShots());
System.out.printf("威胁率: %.1f%%%n", unitedStats.getThreatRate());
// 按区域分析
System.out.println("\n=== 按区域威胁分析 ===");
Map<Integer, List<ThrowInEvent>> areaMap = analyzer.analyzeByArea();
areaMap.forEach((area, events) -> {
System.out.printf("区域%d: %d次威胁%n", area, events.size());
});
}
// 生成测试数据
private static List<ThrowInEvent> generateTestData() {
List<ThrowInEvent> events = new ArrayList<>();
Random random = new Random();
String[] teams = {"曼联", "曼城", "利物浦"};
String[] outcomes = {"普通", "普通", "普通", "传中", "射门", "进球"};
// 生成30个随机事件
for (int i = 0; i < 30; i++) {
LocalTime time = LocalTime.of(0, random.nextInt(90), random.nextInt(60));
int area = random.nextInt(6) + 1;
String team = teams[random.nextInt(teams.length)];
boolean isDeepThrow = random.nextBoolean();
String outcome = outcomes[random.nextInt(outcomes.length)];
events.add(new ThrowInEvent(time, area, team, isDeepThrow, outcome));
}
return events;
}
}
扩展功能(可选)
// 更高级的分析功能
class AdvancedAnalyzer extends ThrowInAnalyzer {
// 统计特定时间段的威胁
public Map<String, Long> analyzeThreatByHalf() {
return allEvents.stream()
.filter(e -> e.getAttackThreatScore() >= 3)
.collect(Collectors.groupingBy(
e -> e.getTime().getMinute() < 45 ? "上半场" : "下半场",
Collectors.counting()));
}
// 分析深度掷球和普通掷球的威胁率对比
public String compareThrowInTypes() {
long deepThrowCount = allEvents.stream()
.filter(ThrowInEvent::isDeepThrow)
.count();
long deepThrowThreat = allEvents.stream()
.filter(e -> e.isDeepThrow() && e.getAttackThreatScore() >= 3)
.count();
long normalThrowCount = allEvents.size() - deepThrowCount;
long normalThrowThreat = allEvents.size() - deepThrowThreat;
return String.format("深度掷球威胁率: %.1f%%%n普通掷球威胁率: %.1f%%",
deepThrowCount > 0 ? (double) deepThrowThreat / deepThrowCount * 100 : 0,
normalThrowCount > 0 ? (double) normalThrowThreat / normalThrowCount * 100 : 0);
}
}
测试输出示例
========== 界外球威胁分析报告 ==========
=== 球队威胁率排名 ===
曼城: 威胁率66.7% (总界外球12, 威胁次数8, 高威胁3, 进球1, 射门2)
利物浦: 威胁率57.1% (总界外球7, 威胁次数4, 高威胁1, 进球0, 射门1)
曼联: 威胁率54.5% (总界外球11, 威胁次数6, 高威胁2, 进球1, 射门1)
=== 高威胁事件详情 (评分≥7) ===
时间:00:12:30 区域:1 球队:曼城 深度掷球:是 结果:进球 威胁值:10
时间:00:45:15 区域:4 球队:曼联 深度掷球:是 结果:射门 威胁值:8
时间:00:67:45 区域:2 球队:曼城 深度掷球:是 结果:射门 威胁值:7
这个案例提供了:
- 数据模型:包含界外球事件的完整信息
- 威胁评分系统:基于区域、深度掷球和结果综合评分
- 多维度统计:按球队、区域、时间段分析
- 可视化输出:生成详细的分析报告
- 可扩展性:支持添加更多分析维度
您可以根据需要调整威胁评分的权重,或者增加更多分析维度(如特定球员、战术执行等)。