Program that says how many days the month has


Programa em Java que recebe o mês e informa quantos dias ele possui.Exercise:

Create a Java program that receives an integer of 1 to 12, representing the months of the year and return the number of days in the month.
Use switch and not use 'case' in it.
Use accumulation case.







Initially, the variable 'days' is declared as having 31 days.

If the four months, 6, 9 or 11, one is subtracted from variable "days" and the program advises that these months possess 30 days.

If it is month 2, is subtracted 2 'days', leaving 28 days for the month of February.
If there is no such month does not fall on the switch, then continue with 31 days (which are 1 month, 3, 5, 7, 8, 10 and 12).

Java code for this exercise:


import java.util.Scanner;

public class months {
    public static void main(String[] args) {
        int month, days=31;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Type the month number [1-12]: ");
        month = input.nextInt();
        
        if(month>12 || month<1){
            System.out.println("Invalid month");
            return;
        }
        
        switch( month )
        {
            case 2: 
                days -=2;
                
            case 4: case 6: case 9: case 11:
                days--;

        }
        
        System.out.printf("The month %d has %d days", month, days);
    }
}

No comments: