We would like to write a program that prompts the user to enter a year and the first three letters of a month name.
Display the number of days in the month.
Here is a sample run:
Enter a year: 2001 Enter a month: Jan Jan 2001 has 31 days Enter a year: 2016 Enter a month: Feb Jan 2016 has 29 days
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter a year and // the first three letter of a month name System.out.print("Enter a year: "); int year = input.nextInt(); System.out.print("Enter a month: "); String month = input.next();/* w w w.ja v a 2 s.c o m*/ // Test for leap year boolean leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); // Display the number of day in the month System.out.print(month + " " + year + " has "); //your code here } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter a year and // the first three letter of a month name System.out.print("Enter a year: "); int year = input.nextInt(); System.out.print("Enter a month: "); String month = input.next(); // Test for leap year boolean leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); // Display the number of day in the month System.out.print(month + " " + year + " has "); if ( month.equals("Jan") || month.equals("Mar") || month.equals("May") || month.equals("Jul") || month.equals("Aug") || month.equals("Oct") || month.equals("Dec")) System.out.println(31 + " days"); else if (month.equals("Apr") || month.equals("Jun") || month.equals("Sep") || month.equals("Nov")) System.out.println(30 + " days"); else System.out.println(((leapYear) ? 29 : 28) + " days"); } }