- My Forums
- Tiger Rant
- LSU Recruiting
- SEC Rant
- Saints Talk
- Pelicans Talk
- More Sports Board
- Fantasy Sports
- Golf Board
- Soccer Board
- O-T Lounge
- Tech Board
- Home/Garden Board
- Outdoor Board
- Health/Fitness Board
- Movie/TV Board
- Book Board
- Music Board
- Political Talk
- Money Talk
- Fark Board
- Gaming Board
- Travel Board
- Food/Drink Board
- Ticket Exchange
- TD Help Board
Customize My Forums- View All Forums
- Show Left Links
- Topic Sort Options
- Trending Topics
- Recent Topics
- Active Topics
Started By
Message
re: C# Help. I always have dumb face on.
Posted on 2/4/15 at 5:44 pm to Hu_Flung_Pu
Posted on 2/4/15 at 5:44 pm to Hu_Flung_Pu
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 on 2/5/15 at 10:45 am to Korkstand
quote:
Korkstand
quote:
MamouTiger65
Y'all are awesome. Thanks! I may need your help again.
Popular
Back to top
Follow TigerDroppings for LSU Football News