练习Map集合对象进行查找世界杯冠军年份、地点、夺冠国家

tech2026-04-08  2

//作为对象传进Map里面 class Test13{ private String yearPlay; private String location; public String getYearPlay() { return yearPlay; } public void setYearPlay(String yearPlay) { this.yearPlay = yearPlay; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Test13(String yearPlay, String location) { this.yearPlay = yearPlay; this.location = location; } @Override public String toString() { return "举办年份:" + yearPlay + " 举办地点:" + location ; } } class Test14{ public static void main(String[] args) { Map<Test13, String> map = new HashMap<>(); map.put(new Test13("1930", "乌拉圭"), "乌拉圭"); map.put(new Test13("1934", "意大利"), "意大利"); map.put(new Test13("1938", "法国"), "意大利"); map.put(new Test13("1950", "巴西"), "乌拉圭"); map.put(new Test13("1954", "瑞士"), "西德"); map.put(new Test13("1958", "瑞典"), "巴西"); map.put(new Test13("1931", "乌拉圭"), "乌拉圭"); boolean flag=true; while (true){ System.out.println("*****************查找世界杯********************"); System.out.println("1.输入年份查找"+"\n"+"2.输入举办国家查找"+"\n"+"3.输入当年夺冠国家查找"); System.out.println("**********************************************"); System.out.println("输入你的选择:"); Scanner scanner=new Scanner(System.in); String choiceNum=scanner.nextLine(); switch (choiceNum){ case "1": findYear(map); break; case "2": findCountry(map); break; case "3": findChampionCountry(map); break; case "4": System.exit(0); } } } public static void findYear(@NotNull Map<Test13,String> map){ Set<Map.Entry<Test13, String>> entrySet = map.entrySet(); System.out.println("输入年份:"); Scanner scanner = new Scanner(System.in); String inputYear = scanner.nextLine(); boolean flag = true; while (flag) { Iterator it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<Test13, String> entryYear = (Map.Entry<Test13, String>) it.next(); if (entryYear.getKey().getYearPlay().equals(inputYear)) { System.out.println(entryYear.getKey() + " 冠军是:" + entryYear.getValue()); flag=false; } } if (flag==true) { System.out.println("没有这个时间!"); inputYear=scanner.nextLine(); } } } public static void findCountry(@NotNull Map<Test13,String> map){ Set<Map.Entry<Test13, String>> entrySet = map.entrySet(); System.out.println("输入举办国家:"); Scanner scanner = new Scanner(System.in); String inputCountry = scanner.nextLine(); boolean flag = true; while (flag) { Iterator it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<Test13, String> entryYear = (Map.Entry<Test13, String>) it.next(); if (entryYear.getKey().getLocation().equals(inputCountry)) { System.out.println(entryYear.getKey() + " 冠军是:" + entryYear.getValue()); flag=false; } } if (flag==true) { System.out.println("没有这个国家!"); inputCountry=scanner.next(); } } } public static void findChampionCountry(@NotNull Map<Test13,String> map){ Set<Map.Entry<Test13, String>> entrySet = map.entrySet(); System.out.println("输入夺冠国家:"); Scanner scanner = new Scanner(System.in); String inputCountry = scanner.nextLine(); boolean flag = true; while (flag) { Iterator it = entrySet.iterator(); while (it.hasNext()) { Map.Entry<Test13, String> entryYear = (Map.Entry<Test13, String>) it.next(); if (entryYear.getValue().equals(inputCountry)) { System.out.println(entryYear.getKey() + " 冠军是:" + entryYear.getValue()); flag=false; } } if (flag==true) { System.out.println("没有这个国家!"); inputCountry=scanner.next(); } } } }
最新回复(0)