如何检查地图中的键和值是否匹配

时间:2018-09-28 04:13:29

标签: java data-structures

我的任务是向用户询问国家的大写字母,并检查地图的值和键是否匹配。.我不确定如何浏览地图的功能。因此,我试图从用户那里获取字符串值,检查地图以查看值和键是否匹配,如果匹配,请告诉用户。

我正在尝试嵌入代码,为什么嵌入无法正常工作?我在代码输入中使用了括号

enter code here while(!input.equals("q")){       //ask user for input (the key being the state) and outputting the value at that key(capital)
       System.out.println("What is the capital of "+states[k]);
       input = scanner.nextLine();
       if(map.get(input).equals(capitals[k]))
           System.out.println(input + " is the correct capital city of "+states[k]);
       else
           System.out.println(input + "is NOT the correct capital city of "+states[k]);
       k++;
   }

1 个答案:

答案 0 :(得分:0)

我不确定您要什么。但据我了解,您想从填充的HashMap中进行基本的键值查找。

if(map.get(input) != null && map.get(input).equals(state[k])){
   System.out.println("The capital of "+ input + " is "+map.get(input));
}