import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
int month = scanner.nextInt();
int day = scanner.nextInt();
boolean isLeap = isLeapYear(year);
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeap) {
daysInMonth[1] = 29;
}
int totalDays = 0;
for (int i = 0; i < month - 1; i++) {
totalDays += daysInMonth[i];
}
totalDays += day;
System.out.println(totalDays);
}
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
https://www.nowcoder.com/discuss/727521113110073344
思路:
- 输入处理:使用Scanner读取输入的年、月、日。
- 闰年判断:通过isLeapYear方法判断是否为闰年。闰年规则是能被4整除但不能被100整除,或者能被400整除。
- 月份天数数组:初始化一个非闰年的月份天数数组,如果是闰年,将二月天数改为29天。
- 计算总天数:遍历前面各个月份的天数并累加,再加上当月的日期,输出结果。



京公网安备 11010502036488号