Im getting an error but I do have the correct answer, It feels a bit like a bummer that the correct solution is a switch. But this works also fine.
package com.codegym.task.task04.task0411;
public class Solution {
public static void main(String[] args) {
checkSeason(12);
checkSeason(4);
checkSeason(7);
checkSeason(10);
}
public static void checkSeason(int month) {
if (month == 12 || (month < 3)){
System.out.println("winter");
}
if (month > 2 && month < 6) {
System.out.println("spring");
}
if (month > 6 && month < 9) {
System.out.println("summer");
}
if (month > 8 && month < 12) {
System.out.println("autumn");
}
}
}
How bad is my solution?
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
NaeRae
20 August 2021, 13:41
You're missing a few equal signs causing logical problems,
but my solution was similar to yours,doing it with if and not a switch,and works out,I wonder why CodeGym solves it with a switch solution while we haven't yet learned it.
0
J H U X
17 January 2021, 15:56
You have no if statement for when input = 6. This should fix the error for you. By the way, try to use else if's in between and else at the end. Good luck!
0
Tommy
16 January 2021, 22:23
Your code needs to be adjusted just a little bit. The premise will work but you need to include 6 for your summer months, currently your code ignores June as a summer month OR spring month (month > 6 --> month >= 6)
You should also use if/else if/ else instead of all if's as this will cause some errors since when you call this method it will attempt to apply the call to ALL of the if statements.
Let me know if this makes sense as I'm not the greatest at explaining things sometimes :S
+1
Guadalupe Gagnon
16 January 2021, 18:36
I tried your code with -3 and it output "winter". It should have output nothing.
+1