Comparison of index (get) in Array List not working properly

Asked

Viewed 69 times

3

I’ve been trying to compare a string (right answer) for an alternative 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 every wrong answer given, the alternatives are shuffled and, as long as the person does not respond to the correct alternative, the loop is repeated.

The options - a, b, c, d, e - remains untouchable. Only the text of the alternatives are scrambled. The question is simple but is exemplified.

I’m using the .get() by means of the for which returns me from 0 to 4 for each element of ArrayList. It is at that moment that the comparison by means of the switch next to the variable boolean appears not to be working, despite Collections.shuffle function correctly with every wrong attempt. For example if I test outside the scope of (a,b,c,d and).

Just follow 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: Use of break within the if in switch-case consisted the big code problem, this now tidied up and running with the equals.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points, right after you give your acceptance).

1 answer

1

The code is a little too complicated and even so has errors.

The first thing I did was change the name of the variable that holds the answers because that’s what it is, it’s not a question. I would trade it for a array because the amount of elements is not variable, but then would have to create a function of shuffle() since Java doesn’t have a ready one, and I don’t think it’s time yet, or do conversion, which wouldn’t make sense, so I left it like this.

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

I switched the shuffle place because I should do it before I start and it doesn’t make sense to shuffle again after the person misses. I might even have a statement saying this, but it was not posted in the question and even if there 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();
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I noticed in your code that it already indexes 'automatically' the alternative (a, b, c...) pertinent to each question, but I could not understand in for how it was done. Could you explain? Thank you for your patience!

  • Using math and type conversion, I find the number in the ASCII table and the tract as character.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.