Repetition of Numbers

Asked

Viewed 73 times

-2

Good staff I have the following situation: I have an on-screen account like this: x + 1 = 2, the user has to click on the option that completes the account and I tried to use this code of my previous question, but it didn’t work. In this case, I have to leave a value of x "fixed", and ends up repeating this number.

Follow the code I have:

            esc = numeroDecisao();
            res1 = numeroAleatorio();
            res2 = numeroAleatorio();
            resultadoConta = res1 + res2;

            Set<Integer> respostas = new HashSet<>();
            respostas.add(resultadoConta);

            while (respostas.size() < 4) {
                int a = numeroAleatorio();
                int b = numeroAleatorio();
                respostas.add(a + b);
            }
            respostas.remove(resultadoConta);
            Iterator<Integer> iterator = respostas.iterator();

            if (esc == 1) {
                op1.setText(Integer.toString(res1));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 2) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(res1));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 3) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(res1));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 4) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(res1));
            }
            num2.setText(Integer.toString(res2));
            txt_resultado.setText(Integer.toString(resultadoConta));

In the case, the res1 would be the x of account x + 1 = 2.

  • Put your current code here instead of just pointing to the previous question as if nothing had changed. The link was supposed to be just a detail to explain the context better to anyone who would answer the question, it wasn’t supposed to be practically the whole question.

  • @Victorstafusa, thanks for the tip, I’m still learning how to ask the right questions in the stack. I did the editing and put in the code snippet.

1 answer

0

First, you don’t need these if (esc == 1), if (esc == 2), etc. The answer code of the previous question was made so that you had the same code no matter what the esc. I’ll repeat what I put in my answer to the previous question:

Always prefer to use math than a lot of ifs with copied and pasted code.

Não vou escrever um monte de ifs com código copiado e colado.

Second, let’s simplify the generation of random numbers a little bit:

private static final Random RND = new Random();

private int numeroAleatorio() {
    return RND.nextInt(set * 10) + 1;
}

Third, that you have resultadoConta = res1 + res2 because you were doing a bill like X = A + B, where A and B were the numbers given and X was the unknown value to be answered. Now you have accounts in the format X + A = B, and will probably have other such as X - A = B, A + X = B, A - X = B, A * B = X, X * A = B, A / X = B, AX = B, etc..

To model all these possible accounts, first use a class to represent the operation:

public class Conta {
    private int a;
    private int b;
    private int x;
    private String formato;

    public Conta(int a, int b, int x, String formato) {
        this.a = a;
        this.b = b;
        this.x = x;
        this.formato = formato
                .replace("A", String.valueOf(a))
                .replace("B", String.valueOf(b));
    }

    public int getA() {
        return a;
    }

    public int getB() {
        return a;
    }

    public int getX() {
        return x;
    }

    public String getFormato() {
        return formato;
    }

    public String getTermo1() {
        return formato.split(" ")[0];
    }

    public String getOperacao() {
        return formato.split(" ")[1];
    }

    public String getTermo2() {
        return formato.split(" ")[2];
    }

    public String getResultado() {
        return formato.split(" ")[4];
    }
}

And then use an interface to represent all kinds of possible operations and generate the numbers accordingly:

public interface ModeloConta {
    public Conta fazerConta();
}

And with this interface, generate a lot of implementations in an array:

ModeloConta[] modelos = {
    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int b = numeroAleatorio();
            int x = a + b;
            return new Conta(a, b, x, "A + B = X");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a + x;
            return new Conta(a, b, x, "A + X = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a + x;
            return new Conta(a, b, x, "X + A = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a * x;
            return new Conta(a, b, x, "A * X = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int x = numeroAleatorio();
            int a = numeroAleatorio();
            int b = x - a;
            return new Conta(a, b, x, "X - A = B");
        }
    }
};

If you are using Java 8 or higher, this above code can be simplified for this:

ModeloConta[] modelos = {
    () -> {
        int a = numeroAleatorio();
        int b = numeroAleatorio();
        int x = a + b;
        return new Conta(a, b, x, "A + B = X");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a + x;
        return new Conta(a, b, x, "A + X = B");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a + x;
        return new Conta(a, b, x, "X + A = B");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a * x;
        return new Conta(a, b, x, "A * X = B");
    },

    () -> {
        int x = numeroAleatorio();
        int a = numeroAleatorio();
        int b = x - a;
        return new Conta(a, b, x, "X - A = B");
    }
};

Put in the array all the account templates you have. Then having the templates, you adapt the previous code to choose a template and use. I will also use an array of TextView in case you want more than four answers and also to make sure you don’t want to replace this with a lot of ifs awful:

TextView[] opcoes = {op1, op2, op3, op4 /*, op5, op6, op7, quantas você quiser */};
ModeloConta modeloEscolhido = modelos[RND.nextInt(modelos.length)];
Conta conta = modeloEscolhido.fazerConta();
int respostaCerta = conta.getX();
esc = RND.nextInt(opcoes.length);

Set<Integer> respostas = new HashSet<>();
respostas.add(respostaCerta);

while (respostas.size() < opcoes.length) {
    respostas.add(modeloEscolhido.fazerConta().getX());
}
respostas.remove(conta.getX());
Iterator<Integer> respostasErradas = respostas.iterator();

for (int i = 0; i < opcoes.length; i++) {
    int valor = esc == i ? respostaCerta : respostasErradas.next();
    opcoes[i].setText(Integer.toString(valor));
}

num1.setText(conta.termo1());
sinal.setText(conta.operacao());
num2.setText(conta.termo2());
txt_resultado.setText(conta.resultado());

The variables res1, res2 and resultadoConta should then be deleted. The method numeroDecisao() should also be deleted. I added a TextView calling for sinal to save the desired signal for the operation.

By the way, if you prefer, you can replace num1, sinal, num2 and txt_resultado by a single TextView that will have the entire account. In that case, you would change it:

num1.setText(conta.getTermo1());
sinal.setText(conta.getOperacao());
num2.setText(conta.getTermo2());
txt_resultado.setText(conta.getResultado());

That’s why:

txtConta.setText(conta.getFormato());

Where txtConta would be the TextView in which you would show the account (will appear as also similar to 5 + X = 12). Having this new TextView, you would delete the TextViews num1, num2, sinal and txt_resultado. Could also delete methods getTermo1(), getOperaco(), getTermo2() and getResultado() class Conta.

Browser other questions tagged

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