How can I make a variable wait for the user to press 1 of 4 buttons?

Asked

Viewed 670 times

4

Well, I’ll try to be clearer.

I have a variable called int tentativa and I want it to receive a specific value depending on the button the user presses.

The buttons are btPedra[0], btPedra[1], btPedra[2], btPedra[3].

If the user presses btPedra[0], the variable receives 0.

If the user presses btPedra[1], the variable takes 1. And so on and so forth...

Note: The program must wait for the user to press one of the buttons to proceed.

My code is like this:

for (contador = 0; contador < jogada; ++contador){

    tentativa = 
    // AQUI A VARIAVEL tentativa DEVE RECEBER 0, 1, 2 OU 3 DEPENDENDO DO BOTÃO PRESSIONADO.

    if (tentativa[contador] == sequencia[contador]){
        acertos++;
    } else {
        gameOver();
    }
}

In addition to the button to make the variable receive a value, its color will be changed.

NOTE: I am programming in java for android!

Complete code of the method!

public void inicioJogo() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; jogada <= 50; jogada++) {
                    for (contador = 0; contador < jogada; contador++) {
                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
                            }
                        });
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[sequencia[contador]].setBackgroundResource(imagensNormal[sequencia[contador]]);
                            }
                        });
                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (contador = 0; contador < btPedra.length; ++contador){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[contador].setEnabled(true);
                            }
                        });

                    }
                    for (contador = 0; contador < jogada; ++contador){
                        //AQUI EU PRECISO QUE O PROGRAMA PARE E ESPERE O USUARIO PRESSIONAR UM DOS BOTÕES E DEPENDENDO DO BOTAO PRESSIONADO A VARIAVEL tentativa RECEBA DETERMINADO VALOR!
                        if (tentativa == sequencia[contador]){
                            acertos++;
                        } else {
                            gameOver();
                        }
                    }
                }
            }
        }).start();
    }
  • Why don’t you play your last for out of Thread and when it hits you call Thread again? To interrupt Thread take a look here

2 answers

5

I believe you put the same onClick in everyone?

One output is you compare by id of the component that sends the "clicked", thus:

public void cliqueGenerico(View v) {
    int tentativa;
    switch (v.getId()) {
        case R.id.botaoUm:
            tentativa = 0;
            break;
        case R.id.botaoDois:
            tentativa = 1;
            break;
        case R.id.botaoTres:
            tentativa = 2;
            break;
        case R.id.botaoQuatro:
            tentativa = 3;
            break;
    }

    /* ... seu código restante */
}
  • It seems to be this but I still could not solve my problem, because I want that in a moment of looping it wait the user click on one of the four buttons to then proceed ... I don’t know how to apply to my looping ;/

  • @Michaelmarcel Post the rest of your code, I can help you out

  • ready I put the code of the whole method.

-3

Case I: just put the "try" variable to receive the keyboard value. The loop will wait for the assignment.

Ex por Linguagem:

Language C

scanf("%d", &trial);

Java language

Scanner scan = new Scanner(System.in); try = scan.nextLine();

Thus the loop will execute while the value 4 is not assigned to the trial variable.

Case 2:

of {

}while(trial != 4);

  • your answer is C or java ?

  • In both languages, at the time of posting the answer was all together, but now separated, just check.

Browser other questions tagged

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