I know this is going to seem like an unforgiveable sin, but I have 150 variables, and I want to create an if function that does the following:
if(a != 0 && b != 0 && c != 0....) {
//do this
} else {
//do this instead
}
However, when I set one of the if statement conditions (i.e b!=1), whilst the others are still the same (a!=0, c!=0), the else function is still run, even though it should be the initial //do this instead of the //do this instead (see code snippet above).
My question is:
1. Is there a reason why it is not working, and if there is, how do i fix it?
2. Is there a simpler way to do that without having to list all 150 variables !=0 && !=0 etc..?
Cheers
解决方案
Surely it would be simpler to do something like this:
List values = new ArrayList();
values.add(val1);
values.add(val2);
// And so forth...
boolean pass = true;
for (Integer v : values) {
if (v != 0) {
pass = false;
// You could log which variable failed here...
}
}
if (pass) {
// Do something
} else {
// Do something else
}