Index (get) comparison in Array List not working properly

I've been trying to compare a string (right answer) to an alternative of a question as an example in a project I'm developing, but I can't succeed in all the different ways I try.

The goal is that with each wrong answer given, the alternatives are shuffled and, as long as the person does not answer the correct alternative, the Loop repeats itself.

The options - a, b, c, d, E - remains untouchable. Only the text of the alternatives are shuffled. The question is simple but it is exemplary.

I am using .get() through for which returns me from 0 to 4 for each element of ArrayList. It is at this moment that the comparison through the switch with the variable boolean seems not to work, although the Collections.shuffle works correctly with each wrong attempt. For example if I test outside the scope of ( a,b,c, d, and ).

Next my code:

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    boolean acertou = false;
    List<String> questao = new ArrayList<String>();
    questao.add("Porta OR");
    questao.add("Porta XOR");
    questao.add("Porta AND");
    questao.add("Porta NOR");
    questao.add("Porta NOT");

    String opcaoCorreta = "Porta NOT", opcaoString;
    char opcao;

    do {
        for (int i = 0; i < 5; i++) {
            questao.get(i);
        }

        System.out.println(
                "(Conceitos de Computação) Que porta lógica utilizamos para inverter o sinal lógico recebido?\n");
        System.out.println("a) " + questao.get(0));
        System.out.println("b) " + questao.get(1));
        System.out.println("c) " + questao.get(2));
        System.out.println("d) " + questao.get(3));
        System.out.println("e) " + questao.get(4));

        System.out.println("\nResposta: ");
        opcaoString = input.next();
        opcao = opcaoString.charAt(0);

        switch (opcao) {
        case 'a':
            if (opcaoCorreta.equals(questao.get(0))) {
                acertou = true;
            }
                break;
        case 'b':
            if (opcaoCorreta.equals(questao.get(1))) {
                acertou = true;
            }
                break;
        case 'c':
            if (opcaoCorreta.equals(questao.get(2))) {
                acertou = true;
            }
                break;
        case 'd':
            if (opcaoCorreta.equals(questao.get(3))) {
                acertou = true;
            }
                break;
        case 'e':
            if (opcaoCorreta.equals(questao.get(4))) {
                acertou = true;
            }
                break;
        if (acertou == true) {
            System.out.println("Resposta correta");
        } else {
            System.out.println("Resposta incorreta");
            Collections.shuffle(questao);
        }

    } while (!acertou);

    input.close();
} }

EDIT: using break inside if in switch-case consisted the big problem of the code, this now neat and working with equals.

Author: Paul Heinrich, 2019-10-05

1 answers

The code is a bit too complicated and that's why it has errors.

The first thing I did was change the name of the variable that keeps the answers because that's what it is, it's not a question. I would exchange for a array because the amount of elements is not variable, but then I would have to create a function of shuffle() own since Java does not have a ready, and I think it's not your time yet, or do conversion, which would not make sense, so I left it like this.

Access options can be done by math avoid this whole code. Note how many variables I eliminated, variable is complexity.

I changed the place shuffling because I should do it before I start and it makes no sense to shuffle again after the person mistakes. I could even have a statement saying this, but it was not posted in the question and even if it exists asks to do something illogical.

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean acertou = false;
        List<String> respostas = new ArrayList<String>();
        respostas.add("Porta OR");
        respostas.add("Porta XOR");
        respostas.add("Porta AND");
        respostas.add("Porta NOR");
        respostas.add("Porta NOT");
        int opcaoCorreta = 4;
        Collections.shuffle(respostas);
        while (true) {
            System.out.println("(Conceitos de Computação) Que porta lógica utilizamos para inverter o sinal lógico recebido?");
            for (int i = 0; i < 5; i++) System.out.println(((char)(i + 'a')) + ") " + respostas.get(i));
            System.out.println("Resposta: ");
            char opcao = input.next().charAt(0);
            if (opcao - 'a' == opcaoCorreta) {
                System.out.println("Resposta correta");
                break;
            }
        }
        input.close();
    }
}

See working on ideone. And no repl.it. also I put on GitHub for future reference .

 1
Author: Maniero, 2019-10-07 13:24:30