世界杯冠军问题 |
您所在的位置:网站首页 › 没有举办世界杯的年份 › 世界杯冠军问题 |
问题: 利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输 出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录 历届世界杯冠军 . 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯. 代码如下: 首先创建一个实体类用于存放球队的信息: package practice.resolution3; public class Champion { private int num; private int year; private String place; private String country; public Champion() { // TODO Auto-generated constructor stub } public Champion(int num, int year, String place, String country) { super(); this.num = num; this.year = year; this.place = place; this.country = country; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "Champion [num=" + num + ", year=" + year + ", place=" + place + ", country=" + country + "]"; } }测试类包含球队数据和执行方法: package practice.resolution3; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Scanner; public class TestChampion { private Map cmap; { cmap = new HashMap(); cmap.put(1930,new Champion(1,1930,"乌拉圭","乌拉圭")); cmap.put(1934,new Champion(2,1934,"意大利","意大利")); cmap.put(1938,new Champion(3,1938,"法国","意大利")); cmap.put(1950,new Champion(4,1950,"巴西","乌拉圭")); cmap.put(1954,new Champion(5,1954,"瑞士","西德")); cmap.put(1958,new Champion(6,1958,"瑞典","巴西")); cmap.put(1962,new Champion(7,1962,"智利","巴西")); cmap.put(1966,new Champion(8,1966,"英格兰","英格兰")); cmap.put(1970,new Champion(9,1970,"墨西哥","巴西")); cmap.put(1974,new Champion(10,1974,"前西德","西德")); cmap.put(1978,new Champion(11,1978,"阿根廷","阿根廷")); cmap.put(1982,new Champion(12,1982,"西班牙","意大利")); cmap.put(1986,new Champion(13,1986,"墨西哥","阿根廷")); cmap.put(1990,new Champion(14,1990,"意大利","西德")); cmap.put(1994,new Champion(15,1994,"美国","巴西")); cmap.put(1998,new Champion(16,1998,"法国","法国")); cmap.put(2002,new Champion(17,2002,"韩日","巴西")); cmap.put(2006,new Champion(18,2006,"德国","意大利")); cmap.put(2010,new Champion(19,2010,"南非","西班牙")); cmap.put(2014,new Champion(20,2014,"巴西","德国")); } public void showChampion() { System.out.println("请输入年份:"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); if(s.matches("^\\d{4}$")) { int year = Integer.parseInt(s); Champion v = cmap.get(year); if(v == null) { System.out.println(year+"年未举办世界杯"); }else { System.out.println("冠军队伍"+v); } } else { System.out.println("输入年份有误,请重新输入"); } showChampion(); } public void showYear() { System.out.println("请输入获奖队伍名称:"); Scanner sc = new Scanner(System.in); String team = sc.nextLine(); //获取所有值的集合 Collection c = cmap.values(); //将Collection转换为ArrayList List list = new ArrayList(c); //统计获奖次数 int count = 0; for(Champion ch:list) { if(Objects.equals(team, ch.getCountry())) { System.out.println(team+"获得冠军的年份:"+ch.getYear()); count++; } } if(count == 0) { System.out.println("该队伍没有获得冠军,再接再厉"); }else { showYear(); } } public static void main(String[] args) { TestChampion tc= new TestChampion(); // tc.showChampion(); tc.showYear(); } }测试结果: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |