compare Array with another Array com for in Java

Asked

Viewed 118 times

0

Everybody, Good afternoon.

I am wanting to compare 2 Array, where 1 array has randomly set value. I want to use for so I don’t have to repeat 100 times. But I don’t know, because it always shows me only the last value of the Array; Come on, I want it.

 btQuestions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random randomico = new Random();
            int perguntaaleatoria = randomico.nextInt(perguntas.length);


                 textQuestions.setText(perguntas[perguntaaleatoria]);


for(int i = 0;i < perguntas.length; i++) {
                     for (int j = i; j < respostas.length; j++) {


                         if (perguntas[i].equals(textQuestions.getText())) {
                             textoResposta.setText(respostas[j]);
                         }
                     }

                 }

at the moment it’s like this:

if (perguntas[1].equals(textQuestions.getText())) {
                  textoResposta.setText(respostas[1]);
              }
              if (perguntas[2].equals(textQuestions.getText())) {
                  textoResposta.setText(respostas[2]);
              }
              if (perguntas[3].equals(textQuestions.getText())) {
                  textoResposta.setText(respostas[3]);

In the second it does not present error, more imagine it with 100 questions! OO"

1 answer

1


To iterate all, you must remove the internal loop (j), and thus, make your "comparison in arrays", as you ask:

for(int i = 0;i < perguntas.length; i++) {
    if (perguntas[i].equals(textQuestions.getText())) {
        textoResposta.setText(respostas[i]);
     }
}

But you don’t need this, since you have the issue index stored in int perguntaaleatoria. So you can do it just like this:

int perguntaaleatoria = randomico.nextInt(perguntas.length);
textQuestions.setText(perguntas[perguntaaleatoria]);
textoResposta.setText(respostas[perguntaaleatoria]);
  • sbrubes, Good morning brother! You work with that? I wanted to have this reasoning, I thought I had to have the go, I still stayed about 5 minutes to understand what you did, which as I understood is not comparing the question and the answer, but rather taking the same random int to 2, thus setting both with the same value. Beat up a beat. kkk Brawl bro!! Really worth it.

  • It takes a long time. Stay firm, study and improve yourself! Anything we’re in, hug!

  • Ai sbrubes, I tried to implement to use random question in any class, but then disfigured the comparison, I did so. public void onClick(View v) { textQuestions.setText(perguntas[numberAleatory()]); textoResposta.setText(respostas[numberAleatory()]); public int numberAleatory(){ Random randomico = new Random(); int perguntaaleatoria = randomico.nextInt(perguntas.length); return(perguntaaleatoria); Que fiz errado?

  • Dude.. Every time you call this function numberAleatory will generate a new random number, and then it won’t work at all. You should store this in a variable, but anything put a new question to make it clear to everyone. ;)

  • Brigadão bro, I recovered the value and then it worked, I had even done before. Fight even. You can close the topic as answered, I do not know how it does. Rsrs.

Browser other questions tagged

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