繁体   English   中英

使用用户输入来迭代和填充 LinkedHashMap,避免没有 try-catch 和搜索键的重复

[英]Using user input to iterate and populate a LinkedHashMap, avoiding duplicates without try-catch and searching keys

我正在尝试编写一个抽认卡游戏。 我正在使用 LinkedHashMap 来存储用户定义的卡片(单词、定义)。 当用户尝试添加重复的术语或定义时,请再次询问,直到用户输入唯一的。 一旦cardDefinitioncardName正确,它们就会使用flashcard.put(cardName, cardDefinition)存储在 Map 中。

然后我按添加的顺序询问所有卡片的定义。 如果当前术语的定义是错误的,但另一个术语是正确的,则 output 是原始术语。

代码:

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

所需的 output 示例:

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".

实际 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

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

您好,欢迎来到 stackoverflow。

将“定义”作为键,将“描述”作为值不是更好吗? 这样更容易从定义中获取描述。 你可以做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() + "\""
        );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM