,编写java程序用于记录球员的信息(队名、球员姓名、球员号码)和比赛进球数,实现随着比赛的推进,每进一球循环输入进球的队名、球员号码。最后输出该项足球比赛的球员进球情况和金靴奖得主。
时间: 2024-03-25 08:37:55 浏览: 51
足球比赛统计程序
4星 · 用户满意度95%
好的,我可以为您提供一段Java代码实现这个功能。代码如下:
```java
import java.util.*;
public class SoccerGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Map<Integer, Integer>> playerGoals = new HashMap<>();
Map<Integer, Integer> teamGoals = new HashMap<>();
int maxGoals = Integer.MIN_VALUE;
int goldenBoot = 0;
while (true) {
System.out.print("请输入进球队名:");
String teamName = sc.nextLine();
System.out.print("请输入进球球员号码:");
int playerNumber = sc.nextInt();
sc.nextLine();
if (!playerGoals.containsKey(teamName)) {
playerGoals.put(teamName, new HashMap<>());
}
if (!playerGoals.get(teamName).containsKey(playerNumber)) {
playerGoals.get(teamName).put(playerNumber, 0);
}
int goals = playerGoals.get(teamName).get(playerNumber) + 1;
playerGoals.get(teamName).put(playerNumber, goals);
if (!teamGoals.containsKey(playerNumber)) {
teamGoals.put(playerNumber, 0);
}
int teamGoalsCount = teamGoals.get(playerNumber) + 1;
teamGoals.put(playerNumber, teamGoalsCount);
if (goals > maxGoals) {
maxGoals = goals;
goldenBoot = playerNumber;
}
System.out.print("是否结束比赛?(Y/N)");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println("各球员进球情况:");
for (String teamName : playerGoals.keySet()) {
System.out.println("队名:" + teamName);
for (int playerNumber : playerGoals.get(teamName).keySet()) {
System.out.println("球员号码:" + playerNumber + ",进球数:" + playerGoals.get(teamName).get(playerNumber));
}
}
System.out.println("金靴奖得主是球员号码为:" + goldenBoot + ",进球数为:" + teamGoals.get(goldenBoot));
}
}
```
这个程序使用了Map来记录队名、球员号码和进球数的对应关系,利用循环不断输入进球信息。最后输出球员进球情况和金靴奖得主。希望这个程序能够满足您的需求。
阅读全文