本文目录导读:

我来为你设计一个Java案例,用于统计区域防守漏洞出现的次数,这个案例将模拟一个足球比赛或网络安全场景中的区域防守监控。
完整Java案例:区域防守漏洞统计系统
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;
/**
* 区域防守漏洞统计系统
* 模拟监控多个防守区域,统计漏洞出现次数
*/
public class DefenseVulnerabilityCounter {
// 防守区域枚举
enum DefenseZone {
ZONE_A("前场左路", 1),
ZONE_B("前场中路", 2),
ZONE_C("前场右路", 3),
ZONE_D("中场左路", 4),
ZONE_E("中场中路", 5),
ZONE_F("中场右路", 6),
ZONE_G("后场左路", 7),
ZONE_H("后场中路", 8),
ZONE_I("后场右路", 9);
private final String description;
private final int zoneId;
DefenseZone(String description, int zoneId) {
this.description = description;
this.zoneId = zoneId;
}
public String getDescription() {
return description;
}
public int getZoneId() {
return zoneId;
}
}
// 漏洞严重程度枚举
enum VulnerabilityLevel {
LOW("低危", 1),
MEDIUM("中危", 2),
HIGH("高危", 3),
CRITICAL("严重", 4);
private final String description;
private final int level;
VulnerabilityLevel(String description, int level) {
this.description = description;
this.level = level;
}
public String getDescription() {
return description;
}
public int getLevel() {
return level;
}
}
// 防守漏洞记录类
static class VulnerabilityRecord {
private final DefenseZone zone;
private final VulnerabilityLevel level;
private final LocalDateTime time;
private final String description;
public VulnerabilityRecord(DefenseZone zone, VulnerabilityLevel level, String description) {
this.zone = zone;
this.level = level;
this.time = LocalDateTime.now();
this.description = description;
}
public DefenseZone getZone() {
return zone;
}
public VulnerabilityLevel getLevel() {
return level;
}
public LocalDateTime getTime() {
return time;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return String.format("[%s] %s - %s (%s): %s",
time.format(formatter), zone.getDescription(), level.getDescription(),
zone.name(), description);
}
}
// 防守漏洞统计器
static class VulnerabilityStatistics {
private Map<DefenseZone, Integer> zoneCountMap = new EnumMap<>(DefenseZone.class);
private Map<VulnerabilityLevel, Integer> levelCountMap = new EnumMap<>(VulnerabilityLevel.class);
private Map<String, Integer> zoneLevelCountMap = new HashMap<>();
private List<VulnerabilityRecord> allRecords = new ArrayList<>();
private int totalCount = 0;
// 添加漏洞记录
public void addVulnerability(VulnerabilityRecord record) {
allRecords.add(record);
totalCount++;
// 统计各区域漏洞数
zoneCountMap.merge(record.getZone(), 1, Integer::sum);
// 统计各严重程度漏洞数
levelCountMap.merge(record.getLevel(), 1, Integer::sum);
// 统计区域+严重程度的组合
String key = record.getZone().name() + "_" + record.getLevel().name();
zoneLevelCountMap.merge(key, 1, Integer::sum);
}
// 获取总漏洞数
public int getTotalCount() {
return totalCount;
}
// 获取指定区域的漏洞数
public int getZoneVulnerabilityCount(DefenseZone zone) {
return zoneCountMap.getOrDefault(zone, 0);
}
// 获取指定严重程度的漏洞数
public int getLevelVulnerabilityCount(VulnerabilityLevel level) {
return levelCountMap.getOrDefault(level, 0);
}
// 打印统计报告
public void printStatisticsReport() {
System.out.println("\n========== 区域防守漏洞统计报告 ==========");
System.out.println("统计时间:" + LocalDateTime.now().format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
System.out.println("总漏洞数:" + totalCount);
// 按区域统计
System.out.println("\n【按区域统计】");
System.out.println("-----------------------------");
System.out.printf("%-15s %-10s %s%n", "区域", "漏洞数", "占比");
System.out.println("-----------------------------");
for (DefenseZone zone : DefenseZone.values()) {
int count = getZoneVulnerabilityCount(zone);
double percentage = totalCount > 0 ? (count * 100.0 / totalCount) : 0;
System.out.printf("%-15s %-10d %.1f%%%n",
zone.getDescription(), count, percentage);
}
// 按严重程度统计
System.out.println("\n【按严重程度统计】");
System.out.println("-----------------------------");
System.out.printf("%-10s %-10s %s%n", "严重程度", "漏洞数", "占比");
System.out.println("-----------------------------");
for (VulnerabilityLevel level : VulnerabilityLevel.values()) {
int count = getLevelVulnerabilityCount(level);
double percentage = totalCount > 0 ? (count * 100.0 / totalCount) : 0;
System.out.printf("%-10s %-10d %.1f%%%n",
level.getDescription(), count, percentage);
}
// 找出漏洞最多的区域
System.out.println("\n【漏洞最多区域TOP3】");
List<Map.Entry<DefenseZone, Integer>> sortedByZone =
new ArrayList<>(zoneCountMap.entrySet());
sortedByZone.sort(Map.Entry.comparingByValue(Collections.reverseOrder()));
for (int i = 0; i < Math.min(3, sortedByZone.size()); i++) {
Map.Entry<DefenseZone, Integer> entry = sortedByZone.get(i);
System.out.printf("第%d名: %s - %d次%n",
i + 1, entry.getKey().getDescription(), entry.getValue());
}
// 显示最近10条漏洞记录
System.out.println("\n【最近漏洞记录】(最近10条)");
System.out.println("-----------------------------");
int startIndex = Math.max(0, allRecords.size() - 10);
for (int i = allRecords.size() - 1; i >= startIndex; i--) {
System.out.println(allRecords.get(i));
}
}
// 导出统计结果
public Map<String, Object> exportStatistics() {
Map<String, Object> result = new HashMap<>();
Map<String, Integer> zoneStats = new HashMap<>();
Map<String, Integer> levelStats = new HashMap<>();
for (DefenseZone zone : DefenseZone.values()) {
zoneStats.put(zone.name(), getZoneVulnerabilityCount(zone));
}
for (VulnerabilityLevel level : VulnerabilityLevel.values()) {
levelStats.put(level.name(), getLevelVulnerabilityCount(level));
}
result.put("totalCount", totalCount);
result.put("zoneStats", zoneStats);
result.put("levelStats", levelStats);
result.put("zoneLevelStats", new HashMap<>(zoneLevelCountMap));
return result;
}
}
// 漏洞检测模拟器
static class VulnerabilityDetector {
private final VulnerabilityStatistics statistics;
private final Random random = new Random();
public VulnerabilityDetector(VulnerabilityStatistics statistics) {
this.statistics = statistics;
}
// 模拟检测到漏洞
public void detectVulnerability() {
DefenseZone zone = DefenseZone.values()[random.nextInt(DefenseZone.values().length)];
VulnerabilityLevel level = generateRandomLevel();
String description = generateVulnerabilityDescription(zone, level);
VulnerabilityRecord record = new VulnerabilityRecord(zone, level, description);
statistics.addVulnerability(record);
}
// 生成随机严重程度(带有权重)
private VulnerabilityLevel generateRandomLevel() {
int chance = random.nextInt(100);
if (chance < 10) return VulnerabilityLevel.CRITICAL; // 10%
if (chance < 30) return VulnerabilityLevel.HIGH; // 20%
if (chance < 60) return VulnerabilityLevel.MEDIUM; // 30%
return VulnerabilityLevel.LOW; // 40%
}
// 生成漏洞描述
private String generateVulnerabilityDescription(DefenseZone zone, VulnerabilityLevel level) {
String[] descriptions = {
"防守队员站位失位",
"区域补防不及时",
"传球路线被突破",
"防守协同不足",
"对抗能力薄弱",
"盯人不紧",
"控球失误",
"回防速度过慢",
"防守阵型混乱",
"一对一防守失败"
};
return String.format("%s区域出现%s漏洞: %s",
zone.getDescription(), level.getDescription(),
descriptions[random.nextInt(descriptions.length)]);
}
}
// 主方法 - 演示程序
public static void main(String[] args) {
System.out.println("==============================");
System.out.println(" 区域防守漏洞统计系统");
System.out.println("==============================");
// 创建统计器
VulnerabilityStatistics statistics = new VulnerabilityStatistics();
VulnerabilityDetector detector = new VulnerabilityDetector(statistics);
// 模拟一段时间的防守漏洞检测
int simulationRounds = 5;
int vulnerabilitiesPerRound = 20;
System.out.println("开始模拟防守漏洞检测...");
System.out.println("模拟轮次:" + simulationRounds);
System.out.println("每轮检测次数:" + vulnerabilitiesPerRound);
System.out.println("==============================");
for (int round = 1; round <= simulationRounds; round++) {
System.out.println("\n第" + round + "轮防守检测开始...");
for (int i = 0; i < vulnerabilitiesPerRound; i++) {
detector.detectVulnerability();
}
System.out.println("第" + round + "轮检测完成,累计漏洞数:" + statistics.getTotalCount());
// 每轮之间模拟时间间隔
try {
Thread.sleep(100); // 模拟100毫秒时间间隔
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// 输出详细统计报告
statistics.printStatisticsReport();
// 导出统计数据供进一步分析
Map<String, Object> exportData = statistics.exportStatistics();
System.out.println("\n【数据导出结果】");
System.out.println("可导出的数据键:" + exportData.keySet());
// 查询特定区域的漏洞情况
System.out.println("\n【区域查询示例】");
DefenseZone queryZone = DefenseZone.ZONE_H; // 后场中路
System.out.println(queryZone.getDescription() + "漏洞次数: " +
statistics.getZoneVulnerabilityCount(queryZone));
VulnerabilityLevel queryLevel = VulnerabilityLevel.HIGH;
System.out.println(queryLevel.getDescription() + "等级漏洞次数: " +
statistics.getLevelVulnerabilityCount(queryLevel));
System.out.println("\n==============================");
System.out.println("统计结束,谢谢使用!");
System.out.println("==============================");
}
}
运行示例输出
==============================
区域防守漏洞统计系统
==============================
开始模拟防守漏洞检测...
模拟轮次:5
每轮检测次数:20
==============================
第1轮防守检测开始...
第1轮检测完成,累计漏洞数:20
第2轮防守检测开始...
第2轮检测完成,累计漏洞数:40
...
========== 区域防守漏洞统计报告 ==========
统计时间:2024-01-15 10:30:45
总漏洞数:100
【按区域统计】
-----------------------------
区域 漏洞数 占比
-----------------------------
前场左路 11 11.0%
前场中路 9 9.0%
...
【按严重程度统计】
-----------------------------
严重程度 漏洞数 占比
-----------------------------
低危 42 42.0%
中危 28 28.0%
高危 21 21.0%
严重 9 9.0%
核心功能说明
- 区域管理:定义了9个防守区域
- 漏洞级别:4个严重程度等级
- 统计功能:
- 按区域统计
- 按严重程度统计
- 组合统计
- 导出统计数据
- 模拟检测:自动生成漏洞记录
这个案例提供了完整的区域防守漏洞统计解决方案,可根据实际需求调整区域划分和统计维度。