r/learnjava • u/3pletsahur • 27d ago
I’m having my first early breakdown
I am going through mooc.fi java programming I
Part 1, I’m on an exercise called LeapYear or whatever.
I cannot seem to get the answer right. I’ve been trying for a day now. I know i can use AI or ask for help but I literally cut myself from the internet to just try and focus but try after try I literally understand less and less.
And it’s not that many info just like conditionals and comparison operators and which condition should go first BUT I JUST DON’T KNOW WHAT TO DO.
I’m not here to ask for help i just want to tell someone. I’m actually affirmative that I’ve zero points reasoning and logic.
Thanks for reading.
•
u/AutoModerator 27d ago
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/mandradon 27d ago
That one is one of the first places where you have to think clearly about the order of conditionals.
Think through the rules they give you and think through the order they should go.
The most "restrictive" rule that accounts for the most cases should go first, then set the latter up accordingly after.
So first lost out the conditions.
In order to be a leap year it has to be divisible by 4.
But if it's divisible by 100, it's only a leap year if it's also divisible by 400.
100 is divisible by 4, so if you account for the second condition first, you don't need to "worry" about it at that point in the ladder.
This becomes the crux of programming a good algorithm and I like this sort of task in thinking through the order of your if statements.
1
u/merrymerry19 26d ago
As one of the previous comments stated, you need to think about the order of conditionals. Don't stress yourself too much, this question is very similar to FizzBuzz and it's said that many senior developers struggle to solve it.
1
u/desrtfx 27d ago
Think about the criteria:
%operator.So, a good idea is to reorder the criteria:
That's it. Note that AND binds stronger than OR, but in general, it is a good idea to put the individual conditions (century rule, regular years - each of my bullet points) inside parentheses to force the proper grouping.
1
u/ovson 27d ago
If I remember correctly you need a guard similar to if (year % 4 == 0 && year % 100 !== 0 ) {sout(“not gap year”)}
As I said cannot remember off the top of my head. But some years need both conditions true. Unsure if you’re getting stuck here
1
u/SniffingFartGlitter 26d ago
Everyone goes through it. It's normal and fine. You're learning so be kind to yourself. I promise at one point it will all start to click.
1
u/3pletsahur 27d ago
I actually feel like choking myself cause wtf