主队胜率统计 - Java 案例
下面是一个完整的 Java 案例,用于统计足球(或篮球)比赛中主队胜率,包含数据模型、计算逻辑和示例输出。

完整代码
import java.util.ArrayList;
import java.util.List;
/**
* 主队胜率统计案例
*/
public class HomeWinRateCalculator {
// 比赛结果枚举
enum Result { HOME_WIN, DRAW, AWAY_WIN }
// 比赛数据模型
static class Match {
String homeTeam;
String awayTeam;
int homeScore;
int awayScore;
Match(String home, String away, int h, int a) {
this.homeTeam = home;
this.awayTeam = away;
this.homeScore = h;
this.awayScore = a;
}
Result getResult() {
if (homeScore > awayScore) return Result.HOME_WIN;
if (homeScore == awayScore) return Result.DRAW;
return Result.AWAY_WIN;
}
}
// 统计结果
static class Stats {
int total;
int homeWins;
int draws;
int awayWins;
double homeWinRate() { return total == 0 ? 0 : (double) homeWins / total * 100; }
double drawRate() { return total == 0 ? 0 : (double) draws / total * 100; }
double awayWinRate() { return total == 0 ? 0 : (double) awayWins / total * 100; }
}
public static Stats calculate(List<Match> matches) {
Stats s = new Stats();
s.total = matches.size();
for (Match m : matches) {
switch (m.getResult()) {
case HOME_WIN -> s.homeWins++;
case DRAW -> s.draws++;
case AWAY_WIN -> s.awayWins++;
}
}
return s;
}
public static void main(String[] args) {
// 模拟 20 场比赛数据
List<Match> matches = new ArrayList<>();
matches.add(new Match("曼城", "利物浦", 2, 1)); // 主胜
matches.add(new Match("阿森纳", "切尔西", 1, 1)); // 平
matches.add(new Match("热刺", "曼联", 0, 3)); // 客胜
matches.add(new Match("拜仁", "多特", 4, 0)); // 主胜
matches.add(new Match("皇马", "巴萨", 3, 2)); // 主胜
matches.add(new Match("巴黎", "马赛", 0, 0)); // 平
matches.add(new Match("尤文", "国米", 1, 2)); // 客胜
matches.add(new Match("米兰", "罗马", 2, 0)); // 主胜
matches.add(new Match("马竞", "塞维利亚", 1, 0)); // 主胜
matches.add(new Match("斯图加特", "莱比锡", 2, 2)); // 平
matches.add(new Match("勒沃库森", "法兰克福", 3, 1)); // 主胜
matches.add(new Match("那不勒斯", "亚特兰大", 0, 1)); // 客胜
matches.add(new Match("里昂", "摩纳哥", 2, 1)); // 主胜
matches.add(new Match("本菲卡", "波尔图", 1, 1)); // 平
matches.add(new Match("阿贾克斯", "埃因霍温", 3, 0)); // 主胜
matches.add(new Match("凯尔特人", "流浪者", 0, 2)); // 客胜
matches.add(new Match("波鸿", "门兴", 1, 3)); // 客胜
matches.add(new Match("水晶宫", "富勒姆", 2, 0)); // 主胜
matches.add(new Match("布莱顿", "纽卡", 1, 1)); // 平
matches.add(new Match("维拉", "西汉姆", 2, 1)); // 主胜
Stats stats = calculate(matches);
System.out.printf("总场次 : %d%n", stats.total);
System.out.printf("主胜 : %d (%.2f%%)%n", stats.homeWins, stats.homeWinRate());
System.out.printf("平局 : %d (%.2f%%)%n", stats.draws, stats.drawRate());
System.out.printf("客胜 : %d (%.2f%%)%n", stats.awayWins, stats.awayWinRate());
System.out.println("---------------------------------");
System.out.printf("主队胜率约 : %.2f%%%n", stats.homeWinRate());
}
}
运行结果
总场次 : 20
主胜 : 10 (50.00%)
平局 : 5 (25.00%)
客胜 : 5 (25.00%)
---------------------------------
主队胜率约 : 50.00%
关键点说明
- 数据模型:
Match封装主客队名和比分,用getResult()判定胜/平/负。 - 核心算法:遍历比赛,按结果累加
homeWins / draws / awayWins。 - 胜率公式:
主胜场次 / 总场次 × 100%。 - 可扩展:可加"真实比赛数据来源"(如 CSV / 数据库)替换硬编码列表。
主队胜率大概有多少"
足球领域普遍经验数据(各大联赛长期统计):
| 结果类型 | 大致占比 |
|---|---|
| 主胜 | 43% ~ 48% |
| 平局 | 24% ~ 28% |
| 客胜 | 26% ~ 32% |
- 主流联赛(英超、西甲、德甲、意甲)主胜率通常在 44% ~ 47% 之间;
- 主场优势较弱的小联赛可能降到 40% 左右;
- 篮球(NBA)主胜率更高,通常在 55% ~ 60%。
我上面的示例数据是"造"出来的,所以得到 50%,若换成真实赛果数据(比如英超一个赛季 380 场),算出的主胜率会落在 44% ~ 47% 区间,你可以把 matches 列表替换为真实数据(从 CSV 或用 SQL 读取)后再计算。
如需我扩展成"从 CSV 文件读取比赛数据"或"分联赛统计主胜率"的版本,告诉我即可。