Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. If the input is April 11, the output is: spring In addition, check if the string and int are valid (an actual month and day). If the input is invalid, the output is: invalid The dates for each season are spring: March 20-June 20 summer: June 21 - September 21 autumn: September 22 - December 20 winter: December 21 -March 19

Respuesta :

Code:

import java.util.Scanner;   //to get input

public class Season  

//this class designed to obtain appropriate season name

{

public static void main(String[] args)

{

 Scanner inputstring = new Scanner(System.in);

System.out.println("Enter the month and day as integers with a space between Eg. January 21");

 int month = inputstring.nextInt();

 int day = inputstring.nextInt();

//seasons that remains the same for the whole month

 if ( (month == 1) || (month == 2))   //Jan and feb

  System.out.println("season: Winter");

 else if ( (month == 4) || (month == 5))    //april and may

  System.out.println("season: Spring");

 else if ( (month == 7) || (month == 8))    //july & august

  System.out.println("season: Summer");

 else if ( (month == 10)|| (month == 11))    //october & november

   System.out.println("season: Fall");

//Months whose season changes according to the date

 else if ( (month == 3) && (day <= 19 ))

  System.out.println("season: Winter");

 else if ( (month == 3) && (day >= 20 ))

  System.out.println("season: Spring");

 else if ( (month == 6) && (day <= 20 ))

  System.out.println("season: Spring");

 else if ( (month == 6) && (day >= 21 ))

  System.out.println("season: Summer");

 else if ( (month == 9) && (day <= 20 ))

  System.out.println("season: Summer");

 else if ( (month == 9) && (day >= 21 ))

  System.out.println("season: Autumn");

 else if ( (month == 12) && (day <= 21 ))

  System.out.println("season: Autumn");

 else if ( (month == 12) && (day >= 22 ))

  System.out.println("season: Winter");

 }

}

ACCESS MORE