题解 | #计算日期到天数转换#
计算日期到天数转换
http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
1.记住判断闰年的方法;
2.记住每个月的天数,一般2月都是28天,但闰年2月是29天;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String[] s = sc.nextLine().split(" ");
int year = Integer.valueOf(s[0]);
int month = Integer.valueOf(s[1]);
int day = Integer.valueOf(s[2]);
int Feb = 28;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { //判断闰年
Feb = 29;
}
int[] arr = {31,Feb,31,30,31,30,31,31,30,31,30,31};
int sumDay = 0;
for (int i = 0; i < month - 1; i++) {
sumDay += arr[i];
}
sumDay += day;
System.out.println(sumDay);
}
sc.close();
}
} 