java - 使用用户输入迭代并填充 LinkedHashMap,避免重复,无需 try-catch 和搜索键

标签 java foreach user-input linkedhashmap

我正在尝试编写一个抽认卡游戏。我使用 LinkedHashMap 来存储用户定义的卡片(单词、定义)。当用户尝试添加重复的术语或定义时,请再次询问,直到用户输入唯一的术语或定义。一旦 cardDefinitioncardName 正确,它们就会使用 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

我相信我离正确的代码不远了(我希望如此)。我需要一些帮助,请。

最佳答案

您好,欢迎来到 stackoverflow。

将“定义”作为键、“描述”作为值不是更好吗?这样就更容易从定义中获得描述。您可以执行flashcards.get("ankle")并获取脚和腿相遇的 body 部分

底部的 println 看起来不对。我想应该是:

System.out.println("错误的答案。正确的答案是\""+ entry.getValue() + "\",你刚刚 "+ "写下了\""+ 替代的定义.getKey() + "\"");
您可以从中获得替代:

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() + "\""
        );
}

关于java - 使用用户输入迭代并填充 LinkedHashMap,避免重复,无需 try-catch 和搜索键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62003764/

相关文章:

java - Astyanax 的 EntityPersister 和 Collection 更新

java - 获取 EntityManager 用于测试或生产的最佳实践

Java - 创建文件读取器和输出的实例

java - 如何使用 TarsosDSP 从文件中提取 MFCC 数据?

Java List forEach Lambda 表达式 - 第一个元素并遍历所有元素

Java lambda - 修改 forEach 循环中的值

javascript - json foreach数组数组与jquery

string - 为什么从标准输入读取用户输入时我的字符串不匹配?

android - 类似电话的数字软键盘