(1)从键盘输入一个年份,输出该年的世界杯冠军是哪支球队。如果该年没有举办世界杯,则输出:没有举办世界杯。
(2)从键盘输入一支球队的名字,输出该球队夺冠的年份列表。
例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯
代码实现
package Exer.exer12;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import java.util.*;
public class FootBallChampion {
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
public static void main(String[] args) {
Map m = new HashMap();
m.put(1930, "乌拉圭");
m.put(1934, "意大利");
m.put(1938, "意大利");
m.put(1950, "乌拉圭");
m.put(1954, "西德");
m.put(1958, "巴西");
m.put(1962, "巴西");
m.put(1966, "英格兰");
m.put(1970, "巴西");
m.put(1974, "西德");
m.put(1978, "阿根廷");
m.put(1982, "意大利");
m.put(1986, "阿根廷");
m.put(1990, "西德");
m.put(1994, "巴西");
m.put(1998, "法国");
m.put(2002, "巴西");
m.put(2006, "意大利");
m.put(2010, "西班牙");
m.put(2014, "德国");
m.put(2018, "法国");
String country = null;
int year;
boolean flag = true;
while (flag) {
System.out.println("请输入查询索引(1:国家/2:年份/3:退出)");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (a == 1) {
System.out.println("请输入国家");
country = sc.next();
//将m转化为set集合
Set<Map.Entry<Integer, String>> sets = m.entrySet();
if (m.containsValue(country)) {//判断所有value中是否包含输入内容
List list = new ArrayList();
for (Map.Entry entry : sets) {
if (entry.getValue().equals(country)) {
list.add(entry.getKey());
}
}
System.out.println("该国家在以下年份中获得过冠军");
for (Object o : list) {
System.out.println(o);
}
} else {
System.out.println("该国家没有获得过冠军");
}
} else if (a == 2) {
System.out.println("请输入年份");
year = sc.nextInt();
if (m.containsKey(year)) {
System.out.println("该年获得冠军的国家是:" + m.get(year));
} else
System.out.println("没有举办世界杯");
} else
break;
}
}
}