Calculate a date is the first day of the year

static void main public (String [] args) { 
	
	System.out.println ( "receiving a user input a month and date, is calculated from the first days of the year"); 
	System.out.println ( "\ input T after month, press enter, enter the date "); 
	System.out.println (" \ n-enter the number 4 years "); 
	
	the while (to true) { 
		int X; 
		int Day = 0; 
		int CAL = 0; 
		new new Scanner Scanner = Scanner (System.in); 
		int year = scanner.nextInt (); 
		boolean LeapYear = isLeapYear (year); 
		System.out.println ( "Please enter the month:"); 
		int = month The scanner.nextInt () ; 
		
		// leap year February 29 days 
		IF (&& LeapYear month The == 2) { 
			System.out.println ( "Please enter the date:"); 
			Day scanner.nextInt = (); 
			the while (Day> 29 || Day <. 1 ) {
				day = x; 
				System.out.println ( "Make a mistake, re-enter the date: ");
				scanner.nextInt = X ();% 2 == 0 month The) { 
			System.out.println ( "Please enter the date:");
			} 
		} 
		
		//-average February 28 days 
		IF (! && LeapYear month The == 2) { 
			System.out.println ( "Please enter the date:"); 
			Day scanner.nextInt = (); 
			the while (Day> 28 Day || <. 1) { 
				System.out.println ( "input error, re-enter the date:"); 
				X = scanner.nextInt (); 
				Day = X; 
			} 
		} 
		
		! IF (month The% 2 = 0) { 
			the System.out. println ( "Please enter the date:"); 
			Day scanner.nextInt = (); 
			the while (Day> Day 31 is || <. 1) { 
				System.out.println ( "input error, re-enter the date:"); 
				X = scanner.nextInt (); 
				Day = X; 
			} 
		} 
		
		IF (! = month The month The 2% 2 == 0 &&) { 
			Day scanner.nextInt = ();
			while (day > 30 || day < 1) {
				System.out.println ( "make a mistake, re-enter the date:"); 
				the X-scanner.nextInt = (); 
				Day = the X-; 
			} 
		} 
		System.out.println ( "Your input is:" + year + "years "+ month +" month "+ day +" day "); 
		CAL = getDays (year, month The, day); 
		System.out.println (year +" of "+ month +" month "+ day +" day is "+ year +" first "+ cal +" days "); 
	} 
} 

/ ** 
 * is a leap year judgment 
 * divisible by 4 and can not be divided by 100, or divisible by 400 
 * @param year 
 * @return 
 * / 
public static Boolean isLeapYear (int year) { 
		
	Boolean = LeapYear to false; 
	IF (! year. 4 == 0 && year% 100% 400% year || = 0 == 0) { 
		LeapYear = to true;
	}
	return leapYear;
}

/**
 * 根据年,月,日,计算总天数
 * @param year
 * @param month
 * @return
 */
public static int getDays(int year, int month, int day) {
	
	int arr[] = {31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30};
	boolean leapYear = isLeapYear(year);
	if (leapYear) {
		arr[1] = 29;
	}
	int sum = 0;
	for (int i = 0; i < month -1; i++) {
		sum += arr[i];
	}
	sum = sum + day;
	return sum;
}

Code logic:

(1) determines whether the input the year is a leap year, criteria: divisible by 4 and can not be divisible by 400 or 100 divisible

(2) Check the month, into the case 4

  a. it is a leap year and February, when February has 29 days

  b. it is a leap year and February, when February has 28 days

  c. leap year, divisible by 2, then the month has 30 days

  d. leap year, can not be divided by 2, then the month has 31 days

Number (3) calculating days, initializes a array of month-average number of days, if it is a leap year, the change in the number of days of February array, cumulative days cycle

  

Guess you like

Origin www.cnblogs.com/xiaobaixie/p/11230676.html