请写java代码。【题目描述】:某公司举行职员足球比赛,编写程序用于记录球员的信息(队名、球员姓名、球员号码)和比赛进球数。实现随着比赛的推进,每进一球循环输入进球的队名、球员号码。最后输出该项足球比赛的球员进球情况和金靴奖得主。 点击复制后,将打开C知道体验页 好的,以下是Java代码:
时间: 2023-12-03 10:45:05 浏览: 47
```
import java.util.*;
public class FootballGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Map<String, Map<Integer, Integer>> goals = new HashMap<>(); // 记录球员进球数,一个嵌套的 Map,第一层键是队名,第二层键是球员号码,值是进球数
int maxGoals = 0; // 最多进球数
List<String> goldenBoots = new ArrayList<>(); // 金靴奖得主的队名和号码,可能有多个
while (true) {
System.out.println("请输入进球的队名和球员号码,或输入q结束输入:");
String team = sc.next();
if (team.equals("q")) {
break;
}
int playerNum = sc.nextInt();
Map<Integer, Integer> teamGoals = goals.getOrDefault(team, new HashMap<>());
int playerGoals = teamGoals.getOrDefault(playerNum, 0);
playerGoals++;
teamGoals.put(playerNum, playerGoals);
goals.put(team, teamGoals);
if (playerGoals > maxGoals) {
maxGoals = playerGoals;
goldenBoots.clear();
goldenBoots.add(team + " 的 " + playerNum);
} else if (playerGoals == maxGoals) {
goldenBoots.add(team + " 的 " + playerNum);
}
}
System.out.println("球员进球情况:");
for (Map.Entry<String, Map<Integer, Integer>> entry : goals.entrySet()) {
System.out.println(entry.getKey() + ":");
for (Map.Entry<Integer, Integer> playerGoal : entry.getValue().entrySet()) {
System.out.println(" " + playerGoal.getKey() + " 号球员:" + playerGoal.getValue() + " 球");
}
}
System.out.println("金靴奖得主:");
for (String goldenBoot : goldenBoots) {
System.out.println(" " + goldenBoot);
}
}
}
```
阅读全文