用Java实现在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯。
时间: 2024-02-13 21:06:16 浏览: 24
好的,你可以使用如下Java代码实现:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class FindYears {
public static void main(String[] args) {
String team = ""; // 存储要查询的球队名字
HashMap<String, ArrayList<String>> matches = new HashMap<>(); // 存储读取的比赛信息
BufferedReader reader = null;
// 读取WorldCupMatches.csv文件
try {
reader = new BufferedReader(new FileReader("WorldCupMatches.csv"));
String line = "";
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
String year = data[0];
String homeTeam = data[1];
String awayTeam = data[2];
String winConditions = data[7];
String winner = "";
if (winConditions.length() > 0) {
winner = winConditions.split(" ")[0];
} else {
int homeGoals = Integer.parseInt(data[3]);
int awayGoals = Integer.parseInt(data[4]);
if (homeGoals > awayGoals) {
winner = homeTeam;
} else if (awayGoals > homeGoals) {
winner = awayTeam;
}
}
if (!matches.containsKey(winner)) {
matches.put(winner, new ArrayList<String>());
}
matches.get(winner).add(year);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 输入要查询的球队名字
System.out.print("请输入球队名字:");
BufferedReader input = new BufferedReader(new java.io.InputStreamReader(System.in));
try {
team = input.readLine().toLowerCase();
} catch (IOException e) {
e.printStackTrace();
}
// 输出该球队夺冠的年份列表
if (matches.containsKey(team)) {
System.out.println("该球队夺冠的年份列表为:" + String.join(" ", matches.get(team)));
} else {
System.out.println("没有获得过世界杯。");
}
}
}
```
请将上述代码保存为一个名为`FindYears.java`的Java源代码文件,并确保与你的`WorldCupMatches.csv`文件在同一目录下。
运行该程序后,输入你想查询的球队名字,即可输出该球队夺冠的年份列表或者“没有获得过世界杯”的提示信息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)