Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int month = Integer.parseInt(sc.nextLine());
- int year = Integer.parseInt(sc.nextLine());
- //check if year is between 1 and 9999
- if(year <= 0 || year > 9999){
- System.out.println("Wrong year!");
- //check if month is between 1 and 12
- }else if(month <= 0 || month > 12){
- System.out.println("Wrong month!");
- }else{
- System.out.println(daysInMonth(month, year));
- }
- }
- public static int daysInMonth(int month, int year){
- boolean dividedByFour = year % 4 == 0;
- boolean dividedByOneHundred = year % 100 != 0;
- boolean dividedByFourHundred = year % 400 == 0;
- int[] month31Days = {1, 3, 5, 7, 8, 10, 12};
- if(dividedByFour && dividedByOneHundred){
- System.out.println("Leap year");
- for(int i = 0; i < month31Days.length; i++){
- if(month == month31Days[i]) return 31;
- else if (month == 2) return 29;
- else return 30;
- }
- }else if(dividedByFour && dividedByFourHundred){
- System.out.println("Leap year");
- for(int i = 0; i < month31Days.length; i++){
- if(month == month31Days[i]) return 31;
- else if (month == 2) return 29;
- else return 30;
- }
- }
- System.out.println("Not a leap year");
- for(int i = 0; i < month31Days.length; i++){
- if(month == month31Days[i]) return 31;
- else if (month == 2) return 28;
- }
- return 30;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement