【发布时间】:2021-09-25 12:21:23
【问题描述】:
我试图解决 Java Hashmaps 的问题,我们需要在其中搜索 key 是否存在。如果是,则打印字符串值,否则打印-1。
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Map<Integer,String> hmap=new HashMap<Integer,String>();
Scanner in=new Scanner(System.in);
int length= in.nextInt();
for(int i=0;i<length;i++) {
hmap.put(in.nextInt(),in.nextLine());
}
while(in.hasNext()) {
int number= in.nextInt();
if(hmap.containsKey(number)) {
String value=hmap.get(number);
System.out.println(value);
}
else {
System.out.println(-1);
}
}
in.close();
}
但是所有的测试用例都没有通过。任何人都可以帮忙解决什么问题吗?
【问题讨论】:
-
对于什么输入不起作用?
标签: java arrays collections hashmap maps