使用用户输入迭代和填充 LinkedHashMap,避免重复而不用 try-catch 和搜索键
Using user input to iterate and populate a LinkedHashMap, avoiding duplicates without try-catch and searching keys
我正在尝试编写一个抽认卡游戏。我正在使用 LinkedHashMap 来存储用户定义的卡片(单词、定义)。当用户尝试添加重复的术语或定义时,再次询问直到用户输入唯一的术语或定义。一旦 cardDefinition
和 cardName
正确,它们将使用 flashcard.put(cardName, cardDefinition)
存储在 Map 中。
然后我按加法顺序询问所有卡片的定义。如果当前术语的定义错误但另一个术语正确,则输出原始术语。
代码:
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() + "\"");
}
}
}
}
}
期望的输出示例:
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".
实际输出:
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
我相信我离正确的代码不远了(我希望如此)。我需要一些帮助。
您好,欢迎来到 Whosebug。
将 'definition' 作为键,将 'description' 作为值不是更好吗?这样就更容易从定义中得到描述。你可以做 flashcards.get("ankle")
并得到 a part of the body where the foot and the leg meet
您在底部的 println
看起来不对。我想应该是:
System.out.println("Wrong answer. The correct one is \"" + entry.getValue() + "\", you've just " + "written the definition of \"" + alternate.getKey() + "\"");
从哪里得到 alternate
:
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() + "\""
);
}
我正在尝试编写一个抽认卡游戏。我正在使用 LinkedHashMap 来存储用户定义的卡片(单词、定义)。当用户尝试添加重复的术语或定义时,再次询问直到用户输入唯一的术语或定义。一旦 cardDefinition
和 cardName
正确,它们将使用 flashcard.put(cardName, cardDefinition)
存储在 Map 中。
然后我按加法顺序询问所有卡片的定义。如果当前术语的定义错误但另一个术语正确,则输出原始术语。
代码:
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() + "\"");
}
}
}
}
}
期望的输出示例:
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".
实际输出:
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
我相信我离正确的代码不远了(我希望如此)。我需要一些帮助。
您好,欢迎来到 Whosebug。
将 'definition' 作为键,将 'description' 作为值不是更好吗?这样就更容易从定义中得到描述。你可以做 flashcards.get("ankle")
并得到 a part of the body where the foot and the leg meet
您在底部的 println
看起来不对。我想应该是:
System.out.println("Wrong answer. The correct one is \"" + entry.getValue() + "\", you've just " + "written the definition of \"" + alternate.getKey() + "\"");
从哪里得到 alternate
:
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() + "\""
);
}