programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Days in the year

Greenhorn
Posts: 27
The year number is given (positive integer). The task is to determine the amount of
days in this year, taking into account that there are 365 days in average year  and 366 days in
leap year . A leap year is a year, which is devided by 4.
Exception is those years that are divided by 100 and are not divided by 400
(For example, the years 300, 1300, and 1900 are not leap years, but 1200 and 2000
are).

Would you  please look through my code and say what is  wrong  with it.

Saloon Keeper
Posts: 4065
48
You are starting off with "is divisible by 4" which means the other tests for 100 and 400 will be skipped. Start with 400 then 100 then 4.

Vital Jays
Greenhorn
Posts: 27
I  wrote
and unfortunately  it doesn't  work properly.
For example, I  enter the year 1200  or 2000 and  compiler shows that it is  not a  leap year, but  it  should  be.

Vital Jays
Greenhorn
Posts: 27
The code is  working, but I did it as  a  shot in the  dark. Can  somebody explain me why after changing here "year%100!=0" from == to != everything works  correctly? Thank you in  advance.

Sheriff
Posts: 11747
191
It would be better for you to understand what you wrote compared to the rules your program enforces:

A year is leap if it is evenly divisible by 4 but not by 100, except if it is also evenly divisible by 400 then it is leap. Otherwise, it isn't leap.

Junilu Lacar
Sheriff
Posts: 11747
191

Junilu Lacar
Sheriff
Posts: 11747
191
By the way, you should put some spaces in that expression. Itsverydifficulttoreadsomethinglikethat. See what I mean?

Vital Jays
Greenhorn
Posts: 27
Thank  you  very  much  for  that link.

And  for  the future I will put some  spaces in  my  text.

Junilu Lacar
Sheriff
Posts: 11747
191

Vital Jays wrote:And  for  the future I will put some  spaces in  my  text.

I wasn't referring to your text, I was referring to your code. And going overboard with the spaces is just as bad as not having any. I was simply trying to make a point. Your response with too many spaces borders on sarcasm (or could be misconstrued as such and thus would not be very well appreciated).

To make it even more readable, you could have extracted the formula to a method:

Vital Jays
Greenhorn
Posts: 27
In other words just  to  do the code  formatting with the button compination  Ctrl+Shift+f.

Ok, in any case thank you.

Junilu Lacar
Sheriff
Posts: 11747
191
No problem. If you refactored the way I showed earlier, you could also just write this: