I have a boolean Array. I want to check if all of this booleans are true.
How can I print a message, if all of them are true?
public static boolean[] tubes = new boolean[9];
I have a boolean Array. I want to check if all of this booleans are true.
How can I print a message, if all of them are true?
public static boolean[] tubes = new boolean[9];
You can loop elements and store its result in a variable as shown below:-
boolean isTrue = false;
for (boolean bool : tubes) {
isTrue = bool;
if (!isTrue) {
break;
}
}
if (isTrue) {
// print...
}
Another solution
boolean isTrue = true;
for (boolean bool : tubes) {
isTrue &= bool;
}
if (isTrue) {
// print...
}
Do you mean like this?
public boolean checkFunction(boolean[] Tubes) {
//boolean[] Tubes = new boolean[9];
boolean et = true; // by default, every member of the array is true
for(int i=0; i < 9;i++) {
if (Tubes[i] == false) { // if i find a false item then its false
System.out.println("This is false: " + Tubes[i]);
et = false;
}
}
return et;
}