我在JSP中使用Hashtables。我想比较两个Hashtable值和键。 First Hashtable包含问题编号(键)和用户答案(值)。第二个哈希表包含问题编号(密钥)和原始答案(值)。我想首先检查问题编号是否匹配然后我想检查相同问题编号的答案。我想要最简单的解决方案。我是学习者,不是专家。 :)
答案 0 :(得分:0)
public static void main(String[] args) {
//Actual Answers
Map<Integer,String> first = new Hashtable<Integer, String>();
first.put(1,"A");
first.put(2,"B");
first.put(3,"C");
//User Answers
Map<Integer,String> second = new Hashtable<Integer, String>();
second.put(1,"A");
second.put(2,"A");
second.put(3,"A");
checkAnswers(first,second);
}
public static void checkAnswers(Map<Integer,String> first, Map<Integer,String> second){
int chkcount = 0;
for(Map.Entry<Integer,String> entry: second.entrySet()){
String actualAnswer = first.get(entry.getKey());
String givenAnswer = entry.getValue();
if(givenAnswer != null && actualAnswer != null && givenAnswer.equals(actualAnswer)){
chkcount = chkcount + 1;
}
}
System.out.println(chkcount);
}