Java 案例:统计射门次数哪队更多
下面是一个完整的 Java 案例,模拟统计两支球队的射门次数并比较哪队更多。

基础版(简单统计)
public class ShotStatistics {
public static void main(String[] args) {
// 模拟两队射门次数
int teamAShots = 15; // A队射门次数
int teamBShots = 12; // B队射门次数
System.out.println("=== 射门次数统计 ===");
System.out.println("A队射门次数: " + teamAShots);
System.out.println("B队射门次数: " + teamBShots);
// 比较
if (teamAShots > teamBShots) {
System.out.println("结果: A队射门次数更多,多 " + (teamAShots - teamBShots) + " 次");
} else if (teamBShots > teamAShots) {
System.out.println("结果: B队射门次数更多,多 " + (teamBShots - teamAShots) + " 次");
} else {
System.out.println("结果: 两队射门次数相同");
}
}
}
面向对象版(推荐)
import java.util.*;
// 球队类
class Team {
private String name;
private List<Integer> shotRecords; // 每场比赛的射门次数
public Team(String name) {
this.name = name;
this.shotRecords = new ArrayList<>();
}
public void addShots(int shots) {
shotRecords.add(shots);
}
public int getTotalShots() {
return shotRecords.stream().mapToInt(Integer::intValue).sum();
}
public double getAverageShots() {
return shotRecords.isEmpty() ? 0 : (double) getTotalShots() / shotRecords.size();
}
public int getMatchCount() {
return shotRecords.size();
}
public String getName() {
return name;
}
public List<Integer> getShotRecords() {
return shotRecords;
}
}
// 主程序
public class ShotComparison {
public static void main(String[] args) {
// 创建两队并录入多场比赛的射门数据
Team teamA = new Team("A队");
Team teamB = new Team("B队");
// 模拟 5 场比赛数据
int[] aShots = {15, 12, 18, 10, 14};
int[] bShots = {10, 14, 11, 13, 12};
for (int s : aShots) teamA.addShots(s);
for (int s : bShots) teamB.addShots(s);
// 输出统计
System.out.println("========== 射门统计报告 ==========");
printTeamStats(teamA);
printTeamStats(teamB);
// 比较总射门数
System.out.println("\n========== 比较结果 ==========");
compareShots(teamA, teamB);
}
private static void printTeamStats(Team team) {
System.out.println("\n【" + team.getName() + "】");
System.out.println(" 比赛场次: " + team.getMatchCount());
System.out.println(" 各场射门: " + team.getShotRecords());
System.out.println(" 总射门数: " + team.getTotalShots());
System.out.printf(" 场均射门: %.2f 次%n", team.getAverageShots());
}
private static void compareShots(Team a, Team b) {
int totalA = a.getTotalShots();
int totalB = b.getTotalShots();
if (totalA > totalB) {
System.out.printf("%s 射门更多!总射门 %d : %d,多 %d 次%n",
a.getName(), totalA, totalB, totalA - totalB);
} else if (totalB > totalA) {
System.out.printf("%s 射门更多!总射门 %d : %d,多 %d 次%n",
b.getName(), totalB, totalA, totalB - totalA);
} else {
System.out.printf("两队射门总数相同:%d : %d%n", totalA, totalB);
}
// 同时比较场均
double avgA = a.getAverageShots();
double avgB = b.getAverageShots();
System.out.printf("场均射门比较: %s %.2f 次 vs %s %.2f 次%n",
a.getName(), avgA, b.getName(), avgB);
}
}
运行结果示例
========== 射门统计报告 ==========
【A队】
比赛场次: 5
各场射门: [15, 12, 18, 10, 14]
总射门数: 69
场均射门: 13.80 次
【B队】
比赛场次: 5
各场射门: [10, 14, 11, 13, 12]
总射门数: 60
场均射门: 12.00 次
========== 比较结果 ==========
A队 射门更多!总射门 69 : 60,多 9 次
场均射门比较: A队 13.80 次 vs B队 12.00 次
| 知识点 | 应用 |
|---|---|
ArrayList |
动态存储每场射门数据 |
stream().mapToInt().sum() |
高效求和 |
| 封装 | Team 类封装数据与行为 |
| 三元运算 / if-else | 比较逻辑 |
printf |
格式化输出 |
扩展思路
- 从控制台输入:使用
Scanner让用户录入每场射门次数 - 从文件读取:用
BufferedReader读取 CSV 比赛数据 - 统计极值:用
Collections.max()找出单场最高射门 - 图形化:用
JFreeChart绘制柱状图对比
需要哪一种扩展版本,可以继续告诉我 😊