DOUBT Android-Do boolean functions

1

I'm doing a function and I want you to return true or false. I want my program to return true when the matrix [pos] has a number greater than 27 and false when it does not. You could help me, and explain to me how I can get true or false.

public boolean matriz(int matri[]) {
    //Creo array de matrices aleatorio
    int i = 10 + (int) Math.round(10 * Math.random());
    int matriz[] = new int[i];

    Log.i("POSICIONES", String.valueOf(matriz.length));

    int pos=0;
    tx1.setText("");
    tx2.setText("");
    // Recorro array de matrices
    for (pos = 0; pos < matriz.length; pos++) {
       //Cada posicion del array almacenara un valor aleatorio;
        matriz[pos] = (int) Math.round(30 * Math.random());
        //El valor aletorio es mayor que 27. Devuelve true, sino devuelve 
        false
        if (matriz[pos] > 27) {
            tx1.setText(tx1.getText() + "" + matriz[pos] + ", ");
        } else {
            tx2.setText(tx2.getText() + "" + matriz[pos] + ", ");
        }
    }
       return true;
}
    
asked by Samuel Silva 15.04.2018 в 20:48
source

1 answer

0

You could create a Boolean variable and store the return there for the case you need to be fulfilled and return it to the end of the program.

public boolean matriz(int matri[]) {

 // variable almacenar retorno
 boolean retorno=false;

//Creo array de matrices aleatorio
int i = 10 + (int) Math.round(10 * Math.random());
int matriz[] = new int[i];

Log.i("POSICIONES", String.valueOf(matriz.length));

int pos=0;
tx1.setText("");
tx2.setText("");
// Recorro array de matrices
for (pos = 0; pos < matriz.length; pos++) {
   //Cada posicion del array almacenara un valor aleatorio;
    matriz[pos] = (int) Math.round(30 * Math.random());
    /**El valor aletorio es mayor que 27. Devuelve true, sino devuelve 
    false**/
    if (matriz[pos] > 27) {
        tx1.setText(tx1.getText() + "" + matriz[pos] + ", ");
        retorno=true;
    } else {
        tx2.setText(tx2.getText() + "" + matriz[pos] + ", ");
        retorno=false;
    }
}

 return retorno;       

}

    
answered by 15.04.2018 в 21:09