我一直试图让这个测验工作一段时间。在我尝试计算用户有多少问题之前,一切似乎都在起作用。似乎"正确"变量不会被添加到应有的位置。它始终显示0。
public class PresidentQuiz {
public static void main(String[] args) {
Integer correct = 0;
Map<Integer, String> president1 = new HashMap<Integer, String>();
president1.put(35, "Kennedy");
president1.put(36, "Johnson");
president1.put(37, "Nixon");
president1.put(38, "Ford");
president1.put(39, "Carter");
president1.put(40, "Reagan");
president1.put(41, "George Bush");
president1.put(42, "Clinton");
president1.put(43, "George W Bush");
president1.put(44, "Obama");
System.out.println("Size of map: " + president1.size());
Map<Integer, String> president2 = new TreeMap<Integer, String>(president1);
Set set1 = president2.entrySet();
Iterator iterator1 = set1.iterator();
while(iterator1.hasNext()) {
Map.Entry me = (Map.Entry)iterator1.next();
System.out.print("President #" + me.getKey() + ": ");
System.out.println(me.getValue());
}
HashSet<Integer> randint = new HashSet<Integer>();
while(randint.size() < 5) {
randint.add((int) (Math.random() * ((44 - 35) + 1) + 35));
}
System.out.println();
System.out.println("Presidents Quiz");
System.out.println();
String pres = null;
Iterator<Integer> iterator2 = randint.iterator();
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
while(iterator2.hasNext()) {
System.out.println("Who was president #" + iterator2.next() + "?");
pres = input.next();
}
while(iterator2.hasNext()) {
String name = president1.get(iterator2.next());
if (name == pres) {
correct += 1;
}
}
// Print correct score
System.out.println("Your score was " + correct);
System.out.println();
// Want to sort names in alphabetical order
System.out.println("Here are the last 10 presidents in alphabetical order:");
}
}