I have a problem, create a class that has a counter and a boolean array, if the position is even and true
, add 1; and if it is odd and false
also sum.
I declared an array and initialized it as 0, that is, {}
, then in the main
I created the array that I use in it, but when I call the class ( nombre_de_la_clase(arreglo)
) it takes 0 that is initialized the arrangement of the class where the meter was.
This is the code:
public class principal {
public static void main (String [] args) {
boolean[] array = {true, false, true, false, true, false, true};
System.out.println (prod(array));
}
public static int prod(boolean [] args) {
boolean[] arreglo = {};
int sum=0;
for (int i=0; i<arreglo.length; ++i) {
if (i%2<=0) {
if (arreglo[i]== true) {
sum++;
}
}
else {
if (arreglo[i]== false) {
sum++;
}
}
}
return sum;
}
}