题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
import java.util.HashMap; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static HashMap<Integer,Integer> daysOfMonth =new HashMap(); static { daysOfMonth.put(1,31); daysOfMonth.put(3,31); daysOfMonth.put(5,31); daysOfMonth.put(7,31); daysOfMonth.put(8,31); daysOfMonth.put(10,31); daysOfMonth.put(12,31); daysOfMonth.put(2,28); daysOfMonth.put(6,30); daysOfMonth.put(9,30); daysOfMonth.put(4,30); daysOfMonth.put(11,30); } public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int year = in.nextInt(); int month = in.nextInt(); int day = in.nextInt(); int days =0; for(int i=1;i<=month-1;i++) { days+=daysOfMonth.get(i); } days+=day; if(isLeapYear(year) && month>2) { days+=1; }; System.out.println(days); } } public static boolean isLeapYear(int year) { return year%400 ==0||year%4==0&&year%100!=0; } }