题目描述
根据输入的日期,计算是这一年的第几天。。
详细描述:
输入某年某月某日,判断这一天是这一年的第几天?。
输入描述:
输入三行,分别是年,月,日
输出描述:
成功:返回outDay输出计算后的第几天; 失败:返回-1
输入例子:
2012
12
31
输出例子:
366
如何判断平年和闰年,可以分为两种情况来看:
1、能被4整除的年份是闰年,不能被4整除的年份是平年。
如:2012和2016是闰年,2014和2015是平年
2、如果是世纪年(即整百年),就只有能被400整除才是闰年,否则就是平年。
如:2000年是闰年,1900年是平年
闰年2月有29天,平年2月只有28天。
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
int nian=scan.nextInt();
int yue=scan.nextInt();
int ri=scan.nextInt();
int sum=0;//记录总天数
//计算总天数
for(int i=1;i<yue;i++){
sum=sum+Givedays(nian,i);
}
System.out.println(sum+ri);
}
}
//判断是闰年还是平年,闰年为true,平年为false,闰年2月29天,平年2月28天
static boolean RunOrPing(int year)
{
if(year%100==0 && year%400==0)
return true;
else if(year%100!=0 && year%4==0)
return true;
else return false;
}
//输入月份返回天数
static int Givedays(int year,int month)
{
int days=0;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;break;
case 4:
case 6:
case 9:
case 11:
days=30;break;
case 2:
if(RunOrPing(year)) days=29;
else days=28;
break;
}
return days;
}
}