Number Repeat

-2

Well, I have the following situation: I have a screen account type like this: x + 1 = 2 , the user has to click on the option that completes the account and I tried to use this code from my previous question , but it did not work. In this case, I have to leave a value of x "fixed", and finish repeating this number.

Here is 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 this case, res1 would be x of account x + 1 = 2 .

    
asked by anonymous 09.12.2017 / 20:49

1 answer

0

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

  

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

Second,let'ssimplifythegenerationofrandomnumbersalittle:

privatestaticfinalRandomRND=newRandom();privateintnumeroAleatorio(){returnRND.nextInt(set*10)+1;}

Third,youhaveresultadoConta=res1+res2becauseyouweremakinganaccountoftypeX=A+B,whereAandBwerethegivennumbersandXwastheunknownvaluetobeanswered.NowyouhaveaccountsintheX+A=Bformat,andyou'llprobablyhaveotherssuchasX-A=B,A+X=B,A-X=B,A*B=X,X*A=B,A/X=B,AX=B.>

Tomodelallthesepossibleaccounts,firstuseaclasstorepresenttheoperation:

publicclassConta{privateinta;privateintb;privateintx;privateStringformato;publicConta(inta,intb,intx,Stringformato){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 sorts 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're using Java 8 or higher, this code can be simplified to 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. Having then the models, you adapt the previous code to choose a model and use. I'll also use an array of TextView if you want more than four replies, and also to make sure you do not want to replace that with a bunch of horrible% s:

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 if , res1 and res2 should then be deleted. The resultadoConta method must also be deleted. I added a numeroDecisao() called TextView to save the desired signal for the operation.

Incidentally, if you prefer, you can replace sinal , num1 , sinal and num2 with a single txt_resultado that will have the entire account. In that case, you would change this:

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

So:

txtConta.setText(conta.getFormato());

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

    
10.12.2017 / 16:49