Java案例:统计"凌空抽射"次数
下面给你一个完整的Java案例,演示如何统计一段文本/比赛记录中"凌空抽射"出现的次数,并判断"多不多"。

场景分析
在足球比赛数据中,射门方式有:凌空抽射、推射、头球、点球、远射等,我们可以统计每场比赛"凌空抽射"的次数,并设定阈值判断是否"多"。
基础版:字符串统计
public class VolleyShotCounter {
/** 统计关键词出现次数 */
public static int countKeyword(String text, String keyword) {
if (text == null || keyword == null || keyword.isEmpty()) {
return 0;
}
int count = 0;
int index = 0;
while ((index = text.indexOf(keyword, index)) != -1) {
count++;
index += keyword.length();
}
return count;
}
public static void main(String[] args) {
String matchRecord = "第10分钟凌空抽射破门,第25分钟推射偏出,"
+ "第40分钟凌空抽射被扑,第60分钟头球攻门,"
+ "第75分钟凌空抽射得分,第88分钟远射";
int volleyCount = countKeyword(matchRecord, "凌空抽射");
System.out.println("凌空抽射次数:" + volleyCount);
// 判断多不多:一场比赛 ≥ 3 次算多
int threshold = 3;
if (volleyCount >= threshold) {
System.out.println("凌空抽射次数较多(≥" + threshold + "次)");
} else {
System.out.println("凌空抽射次数不多");
}
}
}
输出:
凌空抽射次数:3
凌空抽射次数较多(≥3次)
进阶版:面向对象 + 多场比赛统计
import java.util.*;
// 射门记录
class Shot {
String type; // 射门方式
int minute; // 分钟
public Shot(String type, int minute) {
this.type = type;
this.minute = minute;
}
}
public class MatchStat {
// 统计某场比赛某射门方式次数
public static long countByType(List<Shot> shots, String type) {
return shots.stream()
.filter(s -> type.equals(s.type))
.count();
}
public static void main(String[] args) {
List<Shot> match1 = Arrays.asList(
new Shot("凌空抽射", 10),
new Shot("推射", 25),
new Shot("凌空抽射", 40),
new Shot("头球", 60),
new Shot("凌空抽射", 75)
);
List<Shot> match2 = Arrays.asList(
new Shot("推射", 15),
new Shot("点球", 30),
new Shot("远射", 70)
);
int threshold = 3;
long c1 = countByType(match1, "凌空抽射");
long c2 = countByType(match2, "凌空抽射");
System.out.println("第1场凌空抽射:" + c1 + "次 -> " + (c1 >= threshold ? "多" : "不多"));
System.out.println("第2场凌空抽射:" + c2 + "次 -> " + (c2 >= threshold ? "多" : "不多"));
// 全部比赛合计
long total = c1 + c2;
System.out.println("合计凌空抽射:" + total + "次");
}
}
输出:
第1场凌空抽射:3次 -> 多
第2场凌空抽射:0次 -> 不多
合计凌空抽射:3次
实用扩展
| 需求 | 做法 |
|---|---|
| 从文件读取比赛记录 | Files.readAllLines(Paths.get("match.txt")) |
| 统计多种射门方式 | 用 Map<String,Integer> 计数 |
| 判断"多不多" | 设定阈值(如单场≥3次为多,或高于联赛平均) |
| 大小写/别名 | 用正则 Pattern 匹配 "凌空抽射|抽射" |
- 代码层面:统计"凌空抽射"次数很简单,核心是
indexOf循环 或Stream.count()。 - "多不多":不是一个固定答案,取决于阈值设定。
- 单场比赛 ≥ 3 次凌空抽射,可认为较多(这种射门技术难度高,实际比赛一场能有2~3次已属不易);
- 若低于 1~2 次,属于正常/偏少。
- 结合真实足球数据,凌空抽射本身属于低频高难度动作,所以从业务角度看,一场比赛出现多次(≥3)确实是"多"的。
需要我改成读取Excel/CSV比赛数据或接入正则匹配多种射门别名的版本吗?
版权声明:本站文章为原创整理,转载请注明出处。