最后的查找作业
public class FootballDemo {
public static void main(String[] args) {
// 定义HashMap对象
Map<String, String> team = new HashMap<String, String>();
team.put("2014", "德国");
team.put("2010", "西班牙");
team.put("2006", "意大利");
team.put("2002", "巴西");
team.put("1998", "法国");
// 使用迭代器输出方式输出
Iterator it = team.values().iterator();
System.out.println("使用迭代器打印输出国家队:");
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
// 使用EntrySet输出
Set<Entry<String, String>> entrySet = team.entrySet();
System.out.println("使用EnterySet进行输出");
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + "-" + entry.getValue());
System.out.println();
}
// 使用keySet方法查找
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份(key值)");
String strSearch = sc.next();
Set<String> keySet = team.keySet();
String keyo = null;
boolean flag = false;
for (String key : keySet) {
if (strSearch.equals(key)) {
flag = true;
keyo = key;
break;
}
}
if (flag == true) {
System.out.println("找到了");
System.out.println(keyo + "-" + team.get(keyo));
} else
System.out.println("没找到");
}
}13
收起
正在回答 回答被采纳积分+1
1回答
1.Android 零基础入门
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星