题解 | HJ73#计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
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();
int result = 0;
ArrayList<Integer> PingList = new ArrayList<>();
PingList.add(31);
PingList.add(28);
PingList.add(31);
PingList.add(30);
PingList.add(31);
PingList.add(30);
PingList.add(31);
PingList.add(31);
PingList.add(30);
PingList.add(31);
PingList.add(30);
PingList.add(31);
ArrayList<Integer> RunList = new ArrayList<>();
RunList.add(31);
RunList.add(29);
RunList.add(31);
RunList.add(30);
RunList.add(31);
RunList.add(30);
RunList.add(31);
RunList.add(31);
RunList.add(30);
RunList.add(31);
RunList.add(30);
RunList.add(31);
//闰年
if (year > 0 && month <= 12 && day <= 31) {
if (year % 4 == 0&&year % 100 != 0 || year % 400 == 0) {
for (int i1 = 1; i1 < month; i1++) {
result = result + RunList.get(i1 - 1);
}
result = result + day;
} else {
for (int i1 = 1; i1 < month; i1++) {
result = result + PingList.get(i1 - 1);
}
result = result + day;
}
}
System.out.println(result);
}
}
四年一闰 百年不闰 四百年一闰


