Enter the year, month, and day from the keyboard to determine that this day is the day of the year

Enter the year, month, and day from the keyboard to determine that this day is the day of the year

Title description:
Input the year, month, and day separately from the keyboard, and judge that this day is the day of the year.
Note: The standard for judging whether a year is a leap year:
1) Divisible by 4, but not divisible by 100
or
2) Can be divisible by 100 400 divisible

Problem-solving ideas:
Use switch-case structure, but pay attention to whether it is a leap year.

Summary:
1. How to get different types of variables from the keyboard: Need to use the Scanner class
Specific implementation steps:
1. Import package: import java.util.Scanner;
2. Instantiate Scanner: Scanner scan = new Scanner(System.in) ;
3. Call the relevant method of the Scanner class to get the variable of the specified type
Code demonstration:

import java.util.Scanner;//
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();

2. Branch structure 2: switch-case
1. Format

switch(表达式){
    
    
case 常量1
	执行语句1;
	//break;

case 常量2
	执行语句2;
	//break;

...

default:
	执行语句n;
	//break;
}

2. Description:
① According to the value in the switch expression, match the constants in each case in turn. Once the match is successful, it enters the corresponding case structure and calls its execution statement.
After the execution statement is called, the execution statement in other case structures will continue to be executed until the break keyword or the
end of the switch-case structure is encountered .

Break can be used in the switch-case structure, which means that once the keyword is executed, it will jump out of the switch-case structure.
③ The expression in the switch structure can only be one of the following six data types:
byte, short, char , Int, enumeration type (new in JDK5.0), String type (new in JDK7.0)

④ Only constants can be declared after case, not scope.
⑤ The break keyword is optional.
⑥ default: equivalent to else in the if-else structure.
The default structure is optional, and the location is flexible

The Java code for this question:

import java.util.Scanner;

/*
从键盘分别输入年、月、日,判断这一天是当年的第几天

	注:判断一年是否是闰年的标准:
		1)可以被4整除,但不可被100整除
	  或
		2)可以被400整除

*/
public class DayJudge {
    
    
	public static void main(String[] args) {
    
    

		Scanner scan = new Scanner(System.in);
		System.out.println("请输入year:");
		int year = scan.nextInt();
		System.out.println("请输入month:");
		int month = scan.nextInt();
		System.out.println("请输入day:");
		int day = scan.nextInt();

		int sumDays = 0; // 定义一个变量来保存总天数

		switch (month) {
    
    
		case 12:
			sumDays += 30;
		case 11:
			sumDays += 31;
		case 10:
			sumDays += 30;
		case 9:
			sumDays += 31;
		case 8:
			sumDays += 31;
		case 7:
			sumDays += 30;
		case 6:
			sumDays += 31;
		case 5:
			sumDays += 30;
		case 4:
			sumDays += 31;
		case 3:
			// sumDays += 28;
			// 判断year是否是闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    
    
				sumDays += 29;
			} else {
    
    
				sumDays += 28;
			}
		case 2:
			sumDays += 31;
		case 1:
			sumDays += day;
		}
		System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays + "天");
	}
}

Guess you like

Origin blog.csdn.net/qq_45555403/article/details/114129470
Recommended