package com.codegym.task.task04.task0411;
/*
Seasons on Terra
*/
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<=2)
{
System.out.println("winter");
}
if(month >=3&&month<=5)
{
System.out.println("spring");
}
if(month>=6&&month<= 8)
{
System.out.println("summer");
}
if (month>=9&&month<=11)
{
System.out.println("autumn");
}
}
Can somebody explain me what is wrong with my code? It does not compile because it takes the && as the part of a boolean (?)
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Skynet
1 November 2019, 09:40
Once you know about arrays, you can also complete such tasks like this:
String seasons[] = {"winter","spring","summer","autumn"};
System.out.println(seasons[(month % 12) / 3]);
+1
Radames Sanchez
26 October 2019, 19:21
thank you gagnon
0
Guadalupe Gagnon
26 October 2019, 05:27
The assignment operator is the equals '=' sign
To check for equality you need the double equals '=='
Your first if statement you are trying to set month to 12 and not checking for equality and the code crashes because the AND '&&' operator only works with Boolean values and not ints.
a tip to make your code more readable:
use if else statements and you don't need to check the ground and ceiling of every possible variation:
'if-else if' statements work in sequential order and will execute the first if block that is true and stop (or the else block if none of them are true). 0