I am trying to set up an if statement that has a logical condition where two different objects are compared for compatible mating based on their sex. So one needs to be a male and the other female to work. I have set up the genders by using a boolean checking for isMale to be true or not.
Every way I try to set up the condition, I cant get it right.
Here is what I have along with the entire method selecting the objects at random from an arraylist then getting the isMale value stored in each object.
One thing I see in the first if-instruction: you probably meant to use the or-operator ("||") there, but wrote "|". But I think you want to really express something else, in words: "IF mouse1 is male AND mouse2 is NOT male THEN...". You can write that expression very clean by using the getIsMale() methods and the not-operator ("!").
Don't use "== true" or "== false"; simply leave out "== true", and replace "== false" with "!":
I also agree about not needing | - that will evaluate mouse2Sex even if mouse1Sex is already false. Use || unless you really need to evaluate both operands (which is rarely the case).
Thanks for the pointers, I will definetly use the ! to express the true and false conditions instead.
If it was just a simple matter of mouse1 must be a male and mouse2 must be a female to work, then I could put this together. My hang up is that one of the mice just be male and the other must be female. There is no condition that a certain mouse must be a male. I pick two mice at random then check to make one of them is a male and the other is a female, doesnt matter which order. I cant figure out a conditional statement that would make this work.
This may sound absurd but maybe a switch?
The only problem with this idea is mate() will always occur unless I put conditional statement in front of it which what I am trying to solve in the first place.
personally, i think "mouse2Sex" is a bad name for the variable. If I just saw that in the code, I would expect it to hold something like "male" or "female", not "true" or "false". even if I saw it in a condition like "if (mouse2Sex)...", it's not clear what "true" means without digging through the code to find how/where it's set.
I'd suggest something like "isMouse2Male". seeing a variable named that, I'd expect it to be true or false, and I'd know what the values mean.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad: