Java beginners work - write Java programs, enter the date in the console, the calculation date is the first few days of the year corresponding.

Statement of needs:

Write Java programs, enter the date in the console, calculate the date corresponding day of the year.

Realization of ideas:

(1) declare the variable year, month and date, for storing dates in the year, month, day.

(2) variable declarations days, for storing an accumulated number of days.

(3) receiving the variable Scanner year by the date input by the user, and, month, and date assignment.

(4) Enter the first few days of the date is calculated using the current input year, including calculating the number of days in two parts: the accumulated number of days prior to all enter the month 1 month; 2 input month cumulative number of days elapsed. For example, enter the date 2020, May 7. First calculate the number of days for all of 2020 January to April, the number of days in January 2020, corresponding to 31 days, the number of days in February (2020 is a leap year) corresponding to 29 days. March corresponding number of days 31 days 30 days days in April, at this time, can not contain the number of days in May, January 2020 to April all the accumulated number of days to 121 days. Continue to accumulate a few days after seven days of May has passed, and the end result is 2020 May 7 to 128 days of the current year.

(5) The number of days cumulative calculation rules need to loop a plurality of times successively accumulating the number of days per month prior to the month with the use of a for loop structure to achieve.

① expression. Month previous month The need accumulation, the variable i is declared as a loop control variable, according to the operation results, it is recommended to set the initial value of i is 1, it is possible for the loop 3 to the expression:

表达式1:int i = 1
表达式2:i < month
表达式3:i++

② cycle. Using the switch structure, i is calculated corresponding to the month and number of days to accumulate, when i is 2, required is determined leap year.

(6) the use of the results of calculation step, continue to accumulate the number of days elapsed month.

(7) System.out.printlf (); for the formatting operation result is output.

 

Implementation code:

import java.util.Scanner;

public class kkk {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//年月日
		int year,month,date;
		int days = 0;		//天数
		//接收用户输入的年月日
		System.out.println("请输入日期:");
		year = sc.nextInt();
		month = sc.nextInt();
		date = sc.nextInt();
		//使用循环结构,依次累加每月的天数(不包括输入月份当中的月份)
		for(int i = 1; i < month; i++) {
			//使用switch结构,实现每月天数计算
			switch(i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days +=31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days +=30;
				break;
			case 2:
				//闰年判断
				if((year % 4 == 0 && year % 100!=0)||(year % 400 == 0)){
					days += 29;
					break;
				}else {
					days += 28;
					break;
				}
			}
		}
		//累加输入日期中当前月份已过的天数
		days += date;
		System.out.printf("%d年%d月%d日是当前年的第%d天",year,month,date,days);
    }
}

 

Published 34 original articles · won praise 6 · views 1757

Guess you like

Origin blog.csdn.net/weixin_44893902/article/details/104416402