• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with logical condition  RSS feed

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Thanks a lot for the help
 
Aditya Kanitkar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int mouse1 = random.nextInt(mice.length());


Can you exlain about this?

What is random?

if (mouse1Sex == true | mouse2Sex == false)


Why are you using "|" instead of "||"? Is it mandetory?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ("!").
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the exclusive-or operator ^? Surely that situation is exactly what it is designed for?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just feel bad for all the homosexual mice :(
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Any suggestions?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reminder: you're only checking that their genders are different, yes?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, is there a way to check for two different boolean values not matching?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That simplifies things somewhat, no?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Hultin wrote:yes, is there a way to check for two different boolean values not matching?


if mouse1sex != mouse2sex ??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sure, give it away :p
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh doi! That was too obvious, here I was trying to make a simple problem hard... I should be a bureaucrat :P

Thanks a lot for help, I will certainly remember this blow to my ego.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can use booleans after case in a switch block.
The != operator will give a very similar test to the ^ operator I mentioned earlier, but == and != have wider applicability.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!