Write a program that prompts the user to enter a month (1-12) and year, then displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program would display that February 2012 has 29 days. Be careful with February. If the year entered is a leap year, your output should show that February has 29 days. Use the "Case Study: Determining Leap Year" on p. 105 of Introduction to Java Programming for information on how to calculate the leap year. It does not exactly happen every four years.

Respuesta :

Answer:

In Java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    int mnth, yr;

 Scanner input = new Scanner(System.in);

 System.out.print("Month: ");  mnth = input.nextInt();

 System.out.print("Year: ");  yr = input.nextInt();

 boolean lpYear = (yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0);

 if(mnth == 1 || mnth == 3 || mnth == 5 || mnth== 7 || mnth == 8 || mnth == 10 || mnth == 12){

     System.out.print("31 days");  }

 else if(mnth == 2){

     System.out.print(((lpYear) ? "29 days" : "28 days"));  }

else if(mnth == 9 || mnth == 6 || mnth == 4 || mnth== 11){

     System.out.print("30 days");  }

 else{      System.out.print("Invalid");  }

}

}

Explanation:

See attachment for complete program where comments were used as explanation.

Ver imagen MrRoyal