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

wen java案例 6

本文目录导读:

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

  1. 方案一:基于对象列表的统计(最常见)
  2. 方案二:基于二维矩阵地图(网格)统计
  3. 方案三:Java 8+ 流的简洁写法
  4. 如果您的“漏洞”是特定模式(例如:连续命中)

假设您有一个区域地图防守数据列表,需要统计满足特定条件(漏洞)的次数。


基于对象列表的统计(最常见)

假设您有一个防守点的数据对象,每个点有坐标、防守强度等属性。

import java.util.ArrayList;
import java.util.List;
// 1. 定义防守点的实体类
class DefensePoint {
    private String areaId;      // 区域ID
    private double x, y;        // 坐标
    private int strength;       // 防守强度 (1-10)
    private boolean isCovered;  // 是否有队友协防
    public DefensePoint(String areaId, double x, double y, int strength, boolean isCovered) {
        this.areaId = areaId;
        this.x = x;
        this.y = y;
        this.strength = strength;
        this.isCovered = isCovered;
    }
    // Getter 方法(略,根据实际需要添加)
    public String getAreaId() { return areaId; }
    public int getStrength() { return strength; }
    public boolean isCovered() { return isCovered; }
}
// 2. 漏洞检测逻辑
public class DefenseAnalyzer {
    /**
     * 统计防守漏洞次数
     * 规则示例:
     *   1. 防守强度低于3
     *   2. 或者 该点未被协防且强度低于5
     *   3. 或者 连续3个点强度都低于4(连续漏洞)
     */
    public static int countVulnerabilities(List<DefensePoint> points) {
        if (points == null || points.isEmpty()) return 0;
        int vulnerabilityCount = 0;
        int consecutiveWeak = 0; // 连续弱点计数
        for (DefensePoint point : points) {
            boolean isVulnerable = false;
            // ---- 核心漏洞判断逻辑(根据实际业务修改)----
            // 规则1:强度不足
            if (point.getStrength() < 3) {
                isVulnerable = true;
            }
            // 规则2:未协防且强度偏低
            if (!point.isCovered() && point.getStrength() < 5) {
                isVulnerable = true;
            }
            // ---- 判断结束 ----
            if (isVulnerable) {
                vulnerabilityCount++;
                // 连续漏洞逻辑(可选)
                consecutiveWeak++;
                if (consecutiveWeak >= 3) {
                    vulnerabilityCount++; // 额外惩罚或计数
                }
            } else {
                consecutiveWeak = 0; // 重置
            }
        }
        return vulnerabilityCount;
    }
    public static void main(String[] args) {
        // 模拟测试数据
        List<DefensePoint> points = new ArrayList<>();
        points.add(new DefensePoint("A1", 0, 0, 8, true));
        points.add(new DefensePoint("A2", 1, 1, 2, false)); // 漏洞
        points.add(new DefensePoint("A3", 2, 2, 4, false)); // 漏洞(未协防且强度<5)
        points.add(new DefensePoint("B1", 3, 3, 7, true));
        points.add(new DefensePoint("B2", 4, 4, 5, false)); // 非漏洞(强度=5,未协防但规则要求<5)
        points.add(new DefensePoint("B3", 5, 5, 1, false)); // 漏洞
        int count = countVulnerabilities(points);
        System.out.println("检测到防守漏洞次数: " + count);
    }
}

基于二维矩阵地图(网格)统计

如果您有类似网格地图,可以统计每个网格是否为漏洞区。

public class GridDefenseAnalyzer {
    /**
     * 统计漏洞区域
     * @param map 二维数组,数值代表防守强度(0为无人防守,1-5为强度)
     * @return 漏洞次数(强度为0或低于2的格子数量)
     */
    public static int countGridVulnerabilities(int[][] map) {
        int count = 0;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[i].length; j++) {
                if (map[i][j] < 2) { // 假设漏洞条件
                    count++;
                }
            }
        }
        return count;
    }
    public static void main(String[] args) {
        int[][] defenseMap = {
            {5, 4, 0, 1},
            {3, 2, 1, 4},
            {0, 0, 2, 5},
            {1, 3, 4, 6}
        };
        System.out.println("漏洞格子数: " + countGridVulnerabilities(defenseMap));
        // 输出:漏洞格子数: 6
    }
}

Java 8+ 流的简洁写法

import java.util.Arrays;
import java.util.List;
public class StreamAnalyzer {
    public static void main(String[] args) {
        // 假设是防守点强度列表
        List<Integer> strengths = Arrays.asList(8, 2, 4, 7, 5, 1);
        long vulnCount = strengths.stream()
                .filter(s -> s < 3 || s == 1) // 自定义漏洞规则
                .count();
        System.out.println("漏洞出现次数: " + vulnCount);
        // 输出: 2 (值为2和1)
    }
}

如果您的“漏洞”是特定模式(连续命中)

您可能需要回溯算法或状态机,请提供具体的业务描述,我再帮您编写对应的检测逻辑。

请把您的具体“漏洞”判断条件告诉我,我可以帮您微调代码。

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