Java Programming 2. Write if statements to indicate the season based on 2 variab
ID: 3667299 • Letter: J
Question
Java Programming
2. Write if statements to indicate the season based on 2 variables for month (assume integer 1-12) and day (assume integer (1-31). The rules are:
12/16 – 3/15 à winter
3/16 – 6/15 à spring
6/16 – 9/15 à summer
9/16 –12/15 à fall
SOLUTION:
int day, month;
...
SAMPLE OUTPUT:
Enter day value: 1
Enter month value: 12
It is Fall!
Do you want to continue?(y/n): y
Enter day value: 26
Enter month value: 5
It is Spring!
Do you want to continue?(y/n): n
Good bye!
Write if statements to indicate the season based on 2 variables for month (assume integer 1-12) and day (assume integer (1-31). The rules are: 12/16 - 3/15 a winter 3/16 - 6/15 a spring 6/16 - 9/15 a summer 9/16 -12/15 a fall Enter day value: 1 Enter month value: 12 It is Fall! Do you want to continue?(y/n): y Enter day value: 26 Enter month value: 5 It is Spring! Do you want to continue?(y/n): n Good bye!Explanation / Answer
Program:
import java.util.Scanner;
public class Seasons
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int month, day;
String season = null;
System.out.print("Please enter month and day: ");
month = in.nextInt();
day = in.nextInt();
if(1 <= month && month <= 3)
{
season = "Winter";
if((month == 3) && (21 <= day))
season = "Spring";
}
else if (4 <= month && month <=6)
{
season = "Spring";
if((month == 6) && (21 <= day))
season = "Summer";
}
else if (7 <=month && month <=9)
{
season = "Summer";
if((month == 9) && (21 <= day))
season = "Fall";
}
else if (10 <= month && month <= 12)
{
season = "Fall";
if((month == 12) && (21 <= day))
season = "Winter";
}
System.out.println(season);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.