使用用户输入来迭代并填充LinkedHashMap,避免在没有try-catch和搜索键的情况下进行重复

问题描述 投票:0回答:1

我正在尝试编写Flashcard游戏。我正在使用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

我相信我离正确的代码不远了(我希望)。请给我一些帮助。

java foreach user-input linkedhashmap
1个回答
0
投票

您好,欢迎来到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() + "\""
        );
}
© www.soinside.com 2019 - 2024. All rights reserved.