java案例统计凌空抽射次数多不多?

wen java案例 3

统计"凌空抽射"次数的Java案例

下面给你一个完整的Java案例,用数组/集合来统计足球比赛中的"凌空抽射"次数。

java案例统计凌空抽射次数多不多?

场景说明

假设有一场足球比赛记录,每条记录包含球员名、射门类型,我们要统计凌空抽射(volley)一共出现了多少次。


基础版(数组 + for循环)

public class VolleyCounter {
    public static void main(String[] args) {
        // 模拟比赛中的射门记录
        String[] shots = {
            "凌空抽射", "推射", "凌空抽射", "头球", "远射",
            "凌空抽射", "点球", "凌空抽射", "倒钩", "推射"
        };
        int volleyCount = 0;
        // 遍历统计
        for (String shot : shots) {
            if ("凌空抽射".equals(shot)) {
                volleyCount++;
            }
        }
        System.out.println("凌空抽射次数:" + volleyCount);
        System.out.println("射门总次数:" + shots.length);
        // 判断多不多(自定义阈值)
        if (volleyCount >= 3) {
            System.out.println("凌空抽射次数较多!🔥");
        } else {
            System.out.println("凌空抽射次数较少。");
        }
    }
}

输出:

凌空抽射次数:4
射门总次数:10
凌空抽射次数较多!🔥

面向对象版(推荐)

import java.util.*;
import java.util.stream.Collectors;
// 射门记录类
class Shot {
    private String player;
    private String type;
    public Shot(String player, String type) {
        this.player = player;
        this.type = type;
    }
    public String getPlayer() { return player; }
    public String getType()   { return type; }
    @Override
    public String toString() {
        return player + " - " + type;
    }
}
public class VolleyCounter2 {
    public static void main(String[] args) {
        List<Shot> shots = Arrays.asList(
            new Shot("梅西",   "凌空抽射"),
            new Shot("C罗",    "推射"),
            new Shot("姆巴佩", "凌空抽射"),
            new Shot("哈兰德", "头球"),
            new Shot("内马尔", "凌空抽射"),
            new Shot("凯恩",   "点球"),
            new Shot("萨拉赫", "凌空抽射"),
            new Shot("本泽马", "推射")
        );
        // 方法1:循环计数
        long count1 = shots.stream()
                           .filter(s -> "凌空抽射".equals(s.getType()))
                           .count();
        // 方法2:分组统计(可看每种类型数量)
        Map<String, Long> typeStat = shots.stream()
                .collect(Collectors.groupingBy(Shot::getType, Collectors.counting()));
        System.out.println("=== 各类射门统计 ===");
        typeStat.forEach((k, v) -> System.out.println(k + ":" + v + " 次"));
        System.out.println("\n凌空抽射总次数:" + count1);
        // 判断占比
        double ratio = (double) count1 / shots.size();
        System.out.printf("占比:%.2f%%\n", ratio * 100);
        if (ratio >= 0.3) {
            System.out.println("凌空抽射次数偏多 ✅");
        } else {
            System.out.println("凌空抽射次数偏少 ❌");
        }
        // 哪些球员凌空抽射了
        System.out.println("\n凌空抽射球员:");
        shots.stream()
             .filter(s -> "凌空抽射".equals(s.getType()))
             .forEach(s -> System.out.println("  " + s.getPlayer()));
    }
}

输出:

=== 各类射门统计 ===
凌空抽射:4 次
推射:2 次
头球:1 次
点球:1 次
凌空抽射总次数:4
占比:50.00%
凌空抽射次数偏多 ✅
凌空抽射球员:
  梅西
  姆巴佩
  内马尔
  萨拉赫

步骤 说明
存数据 数组 / List / Map 存射门记录
遍历 for 循环 或 Stream
判断 equals("凌空抽射")
计数 count++Stream.count()
判断多不多 阈值占比来判断

扩展:实际应用中"多不多"怎么判断?

一般会结合两种方式:

  1. 绝对数量>= 5 次 算多
  2. 相对占比凌空抽射次数 / 总射门次数 >= 30% 算多
  3. 和对手对比:跟另一支球队或联赛平均值比较

如果你有真实数据(比如某个球队一整个赛季的射门记录表),可以把数据换成 CSV 读取或从数据库查出,逻辑是一样的。

需要我帮你改成读取文件/数据库的版本吗?

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