containsKey & containsValue

Всем привет. Помогите советом.

LinkedHashMap<String, String> cards = new LinkedHashMap<String, String>();
Scanner scanner = new Scanner(System.in);
System.out.println("Input the number of cards:");
// Read the size of the array from the keyboard and write to "size"
int size = Integer.parseInt(scanner.nextLine());
String[] arr = new String[size]; // Create an int array of size "size"
for (int i = 0; i < arr.length; i++) {
    System.out.println("The card #" + (i + 1) + ":");
    String card = scanner.nextLine();
    if (cards.containsKey(card)) {//check if the key exists in the HashMap or not
        System.out.println("The card \"" + card + "\" already exists. Try again:");
        card = scanner.nextLine();
    }
    System.out.println("The definition of the card #" + (i + 1) + ":");
    String definition = scanner.nextLine();
    if (cards.containsValue(definition)) {//check if the value exists in the HashMap or not
        System.out.println("The definition \"" + definition + "\" already exists. Try again:");
        definition = scanner.nextLine();
    }
    cards.put(card, definition);
}
for (Map.Entry<String, String> e : cards.entrySet()) {
    String card = e.getKey();
    String definition = e.getValue();
    System.out.println("Print the definition of \"" + card + "\":");
    String answer = scanner.nextLine();
    if (answer.equals(definition)) {
        System.out.println("Correct answer.");
    } else if (cards.containsValue(answer)) {
        for (Map.Entry<String, String> entry : cards.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (answer.equals(value)) {
                System.out.println("Wrong answer. The correct one is \"" + definition + "\", you've just written the definition of \"" + key + "\".");
            }
        }
    } else {
        System.out.println("Wrong answer. The correct one is \"" + definition + "\".");
    }
}

Задача:

Oтвет неверен для данного термина, но он верен для другого термина.

Спросите все определения карты в порядке добавления. Если определение неверно для текущего термина, но верно для другого, выведите исходный термин.

Когда пользователь пытается добавить дублированный термин или определение, запретите его и спрашивайте снова, пока пользователь не введет уникальный.

Wrong answer in test #3

Your line
`The definition of the card #4:`
should contain `The card "c2" already exists` (ignoring case).

Please find below the output of your program during this failed test.
Note that the '>' character indicates the beginning of the input line.

---

Input the number of cards:
> 4
The card #1:
> c1
The definition of the card #1:
> d1
The card #2:
> c2
The definition of the card #2:
> d2
The card #3:
> c3
The definition of the card #3:
> d3
The card #4:
> c3
The card "c3" already exists. Try again:
> c2
The definition of the card #4:
> c1
Print the definition of "c1":
> c4
Wrong answer. The correct one is "d1".
Print the definition of "c2":
> d2
Wrong answer. The correct one is "c1".
Print the definition of "c3":
> d3
Correct answer.

Ответы (1 шт):

Автор решения: Circassian

Поменяй if (cards.containsKey(card)) на while (cards.containsKey(card)) и if (cards.containsValue(definition)) на while(cards.containsValue(definition))

→ Ссылка