Started By
Message

re: C# Help. I always have dumb face on.

Posted on 2/4/15 at 5:44 pm to
Posted by Korkstand
Member since Nov 2003
28738 posts
Posted on 2/4/15 at 5:44 pm to
quote:

The problem is I don't know why to put
if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 400 == 0))

I know that it is a boolean statement but I wouldn't know how to correctly code it. I would never have guessed to put it in that fashion. If that makes sense.

It can be tricky to just bang out a line like that in one go. It might make more sense to break it down into individual expressions and operations, and then figure out how to work those into boolean logic. And you really need to pay attention to your parentheses, and how they are nested.

This is how I would go about solving this problem. I am only concerned with 4 categories of years: those that are evenly divisible by 400, 100, 4, or none of those. So right off the bat I know I'm going to need these:

year % 400
year % 100
year % 4

Because the modulo operator % returns the remainder, I know that I will need to check if these expressions evaluate to 0 (or not), in order to determine if the year is evenly divisible by these numbers. So let's start like this:

if(year % 4 == 0)

So, if the year is, for example, 2096, 2100, 2104, etc., the remainder when divided by 4 is zero, and 0 == 0 is TRUE, so the if statement will execute and set days = 29. But, of course, 2100 will not be a leap year, so we have to fix that:

if(year % 4 == 0 && year % 100 != 0)

So now we put that AND in there, which means the stuff on both sides of the && have to be TRUE in order for the if to execute. So now for the year 2100, when we divide by 100 the remainder is zero, and 0 != 0 is FALSE. Since this test fails, it doesn't matter that the other side of the && is TRUE, the if statement is still FALSE, so the else will catch it and set days = 28. This is great, except now we have to catch the years that are multiples of 400:

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

Now, if the stuff before the || OR the stuff after the || is TRUE, then the if will execute. So what we've done here is just to add the special case for years that are multiples of 400. The years 2000, 2400, etc will make our if TRUE regardless of what happens on the left of our ||. Likewise, the year 1996, which is evenly divisible by 4 and NOT by 100, will make the entire if be TRUE regardless of what's on the right side of the ||.

And, of course, if the stuff on the left and the stuff on the right are FALSE, only then will our else come into play and set days = 28.



I hope all of this helps, at least a little bit, rather than makes you more confused.
Posted by Hu_Flung_Pu
Central, LA
Member since Jan 2013
22232 posts
Posted on 2/5/15 at 10:45 am to
quote:

Korkstand

quote:

MamouTiger65


Y'all are awesome. Thanks! I may need your help again.
Posted by whodatfan
Member since Mar 2008
21349 posts
Posted on 2/5/15 at 12:04 pm to
quote:

Korkstand


first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram