I'm tryng to write a Flashcard game. I'm using a LinkedHashMap to store cards (word, definition) defined by the user. When the user tries to add a duplicated term or definition, ask again until the user inputs a unique one. Once cardDefinition
and cardName
are correct they are stored in Map using flashcard.put(cardName, cardDefinition)
.
Then I ask all card's definitions in the order of addition. If the definition is wrong for the current term but it is correct for another, output the original term.
Code:
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> flashcards = new LinkedHashMap<>();
// Choose number of cards
System.out.println("Input the number of cards:");
int numberOfCards = Integer.parseInt(scanner.nextLine());
while (flashcards.size() < numberOfCards) {
for (int i = 0; i < numberOfCards; i++) {
System.out.println("The card # " + (i + 1) + ":");
String cardName = scanner.nextLine();
if (flashcards.containsKey(cardName)) {
System.out.println("The card \"" + cardName + "\" already exists. Try again");
cardName = scanner.nextLine();
} else {
System.out.println("The definition of the card #" + (i + 1) + ":");
String cardDefinition = scanner.nextLine();
if (flashcards.containsKey(cardDefinition)) {
System.out.println("The card \"" + cardDefinition + "\" already exists. Try again");
cardDefinition = scanner.nextLine();
}
flashcards.put(cardName, cardDefinition);
}
}
}
System.out.println(flashcards);
System.out.println(flashcards.size());
for (var entry : flashcards.entrySet()) {
System.out.println("Print the definition of " + entry.getKey());
String input = scanner.nextLine();
if (input.equals(entry.getValue())) {
System.out.println("Correct answer.");
} else {
if (flashcards.containsValue(input)) {
System.out.println("Wrong answer. The correct one is \"" + flashcards.get(input) + "\", you've just " +
"written the definition of \"" + entry.getKey() + "\"");
}
}
}
}
}
Desired output example:
Input the number of cards:
> 2
The card #1:
> a brother of one's parent
The definition of the card #1:
> uncle
The card #2:
> a part of the body where the foot and the leg meet
The definition of the card #2:
> ankle
Print the definition of "a brother of one's parent":
> ankle
Wrong answer. The correct one is "uncle", you've just written the definition of "a part of the body where the foot and the leg meet".
Print the definition of "a part of the body where the foot and the leg meet":
> ???
Wrong answer. The correct one is "ankle".
Actual output:
Input the number of cards:
2
The card # 1:
blue
The definition of the card #1:
red
The card # 2:
white
The definition of the card #2:
black
{blue=red, white=black}
2
Print the definition of blue
red
Correct answer.
Print the definition of white
red
Wrong answer. The correct one is "null", you've just written the definition of "white
I believe I'm not far from the correct code (I hope). I would like some help, please.
Hi and welcome to stackoverflow.
Wouldn't it be better to put the 'definition' as the key and the 'description' as the value? That way it is easier to get the description from the definition. you could do flashcards.get("ankle")
and get a part of the body where the foot and the leg meet
Your println
at the bottom looks wrong. I guess it should be:
System.out.println("Wrong answer. The correct one is \"" + entry.getValue() + "\", you've just " + "written the definition of \"" + alternate.getKey() + "\"");
where you get the alternate
from this:
if (flashcards.containsValue(input)) {
Entry<String, String> alternate = null;
for (var alt : flashcards.entrySet()) {
if (alt.getValue().equals(input)) {
alternate = alt;
break;
}
}
System.out.println(
"Wrong answer. The correct one is \"" + entry.getValue() + "\", you've just " +
"written the definition of \"" + alternate.getKey() + "\""
);
}