-3

Hi so I'm stuck here please if someone could help me please I try to explain here the best I can. Code is working fine but it's false.

So I highlighted down (in bold) in the example where the problem is and commented in yellow what should be right for my code.

The values of my LinkedHashMap: (String, String)

cards.put("a", "1");
cards.put("b", "2");
cards.put("c", "3");
cards.put("d", "4");`

Code in action example:

Print the definition of "a"

input> 1

Correct answer

Print the definition of "b"

input> a

Wrong answer

Print the definition of "c"

input> 2

Wrong answer. The correct one is "3", you've just written the definition of "c"

//Here it should be "b" since input was 2 and 2 is the value of key b in the map.

Print the definition of **"c"

//should be "b" here too then it goes all wrong it doesn't match the correct key

input> 3

Correct answer

Print the definition of "d"

input> 1

Wrong answer. The correct one is "4", you've just written the definition of "d" Print the definition of "d"

and here it prints "d" instead of "a" and I tried a lot of things can't know how to fix this.

input> 1

Correct answer

And here is the code source:

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    LinkedHashMap<String, String> cards = new LinkedHashMap<String, String>();

    cards.put("a", "1");
    cards.put("b", "2");
    cards.put("c", "3");
    cards.put("d", "4");

    for (Map.Entry<String, String> entry : cards.entrySet()) {

        String v = entry.getValue();
        String k = entry.getKey();

        System.out.println("Print the definition of " + "\"" + k + "\"");

        String answer = in.nextLine();

        if (answer.equals(v)) {
            System.out.println("Correct answer");

        } else if (!answer.equals(v) && !cards.containsValue(answer)) {
            System.out.println("Wrong answer");

        }

        else if (!answer.equals(v) && cards.containsValue(answer)) {
            System.out.println("Wrong answer. The correct one is " + "\"" + v + "\""
                    + ", you've just written the definition of " + "\"" + k + "\"");
            System.out.println("Print the definition of " + "\"" + k + "\"");

            String answer2 = in.nextLine();

            if (!answer2.equals(v) && !cards.containsValue(answer2)) {
                System.out.println("Wrong answer. The correct one is " + "\"" + v + "\"");

            } else {
                System.out.println("Correct answer");

            }

        }
    }

}
devy
  • 1
  • 1
  • 2

1 Answers1

2
System.out.println("Wrong answer. The correct one is " + "\"" + v + "\""
                    + ", you've just written the definition of " + "\"" + k + "\"");

instead of k, you shall get actual key for the entered value and print that.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33