countTrue.java
This program was created to count the number of how many true boolean values there are.
A boolean cootains two values: true or false.
Function countTrue takes the array of booleans and checks each element to determine if it contains the “true” value. If so, add it to a counter and return it.
The code has been modified slightly and now uploaded to the site.
Code Snippet
public class countTrue{
public static void main(String[] args){
boolean[] arr1 = {true, false, false, true, false};
boolean[] arr2 = {false, false, false, false};
boolean[] arr3 = {};
int result = countTrue(arr1);
int result2 = countTrue(arr2);
int result3 = countTrue(arr3);
System.out.println(result);
System.out.println(result2);
System.out.println(result3);
}
public static int countTrue(boolean[] arr){
int counter = 0;
for (int i = 0; i < arr.length; i++){
if (arr[i] == true){
counter++;
}
}
return counter;
}
}