This is my method to compare ages of two people, coditions are thatwhen a male is under 26 the female must only be 1 year older then him, if over 26 then it does not matter the (answer == true) bit is continued troughout for a condition in the while loop, if that is false it will return that the pair are not a match.
what i want this to do is when i enter the age for a man if it is under26 and i enter a womens age that is 28 or over it will return that the pair are not a match but when i run it, it says they are a match, is their a mistake in that peice of code or is it in the other part, thanks
I absolutly hate big ugly if statements like that, and try to avoid them anywhere I can. Maybe something more like this:
that, to me, looks simpler. In general simpler means better. Does that do what you are looking for?
Joined: Feb 12, 2005
i got it workin, where i had false i put in true and where is had true i put in false, i works now.
ie where i had (answer = true) i changed to (answer = false)
but i did not change it where i had (answer == true)
its all good, my code works for the program but is hugely long, i will change it tomorrow, put in few loops, but i have what i want down and its all goo. as long as it works i am happy. i have been so stressed. and everyone has been great to me. Thank you everyone.
Joined: Dec 29, 2004
having it working is, of course, the most important thing. The point behind making the if statements smaller, less complex, is so that when you come back to it in a week or a few months, it doesn't take you an hour to figure out what you were doing.
Here are a couple of things that might make this easier.
First, readability. Try to think of simple methods you can add to your Person class to let the Person control the details. For example, you haveWhat happens when you change gender to store "M" or "F" or turn it into an integer using constants? Sure, you won't do that in this program, but if you get used to coding defensively, your design will improve.
The Person class is just begging for "boolean isMale()" and "boolean isFemale()"
Second, from what I recall of your ealier description, two people are compatible if and only if one is male and the other is female. So, once you pass that first test, you know you've got one male and one female. Instead of contantly checking which is which, why not set up two local variables to hold them?
Notice how each test has two mirrored if-tests depending on whether "this" happens to be the male or female. This will simplify your if tests later on.Remember, all great programmers are lazy at heart and love to save typing, even if they have to spend hours thinking about a way to save that time. Make sure to cultivate this "skill."
Now, you may be thinking, but "this" is one Person and "N" is the other (What does N that mean?), and you can't very well switch "this". True, that's why you declare two new local Person references. You'll assign "this" and "N" to these new references and use them instead.
In any case, you said you've got it working so just consider these points for future projects. Good job!
Joined: Feb 12, 2005
i know how to do the safe gaurding code but it will take up too much more space. not worth it. thank you everyone, it works perfect, it even is funny!
Joined: Aug 07, 2003
Sooo, are you going to find us all compatible dates now? Err, the single ones of us, anyway!
Originally posted by Valerie String: i know how to do the safe gaurding code but it will take up too much more space. not worth it. thank you everyone, it works perfect, it even is funny!
This would make an interesting time capsule. Take your code from the original post and David's re-worked version of it. Print them both on the same sheet of paper (or mail it to yourself on a yahoo mail account).
Then, compare both, a year from now. Do the same thing two years from now. Each time you compare the two, ask yourself how much space the first one saved you and if it was worth it.
There are very VERY few cases left out there where anything takes precedence over readability. In those cases, you probably won't be using Java anyway.