资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
编写函数求某年某月某日(**** ** **)是这一年的第几天 。提示:要考虑闰年,闰年的2月是29天(闰年的条件:是4的倍数但不是100的倍数,或者是400的倍数)。编写主函数,输入年月日,测试该函数并输出结果。
输入格式
按“yyyy mm dd"的格式输入年月日
输出格式
输出一个整数形式的计算结果
样例输入
1990 5 10
样例输出
130
import java.util.Scanner;
/*
* 1月、3月、5月、7月、8月、10月、12月有31天;4月、6月、9月、11月有30天; 2月是特殊月份,只有28或29天(闰年29)
* */
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();//年
int month = sc.nextInt();//月
int day = sc.nextInt();//日
int days = day;
int[] months = new int[13];
for(int i=1;i<=12;i++){
if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12){
months[i] = 31;
}else{
months[i] = 30;
}
}
if((year%4==0 && year%100!=0) || year%400==0){
months[2] = 29;
}else{
months[2] = 28;
}
for(int i=1;i<month;i++){
days+=months[i];
}
System.out.println(days);
}
}