用户输入年、月和日,根据用户的输入计算出该日期是该年的第几天并输出结果

public static void main(String[] args) {
		// 根据用户输入的年月日  计算出该日期是该年的第几天并输出结果。
		//公历:分为平年和闰年
		//平年31天月份:1 ,3,5,7,8, 10, 12  ;2月28天,4,6,9,11月份30天
		//平年每月的天数
		int [] arr = {31,28,31,30,31,30,31,31,30,31,30,31};
		//闰年天数?  只是2月多了一天
		
		Scanner s = new Scanner(System.in);
		System.out.println("请分别输入年月日:");
		
		int year =s.nextInt();
		int month =s.nextInt();
		int day =s.nextInt();
		
		int sumMonthDays =0;
		//计算本月之前的天数和  
		for(int i=0;i<month-1;i++){
			sumMonthDays+=arr[i];
		}
		/*普通闰年:公历年份是4的倍数的,且不是100的倍数,为闰年。(如2004年就是闰年);
		世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是世纪闰年,2000年是世纪闰年);*/
		if(((year%4==0 && year%100!=0) || year%400==0) && month>2){
			int sumDays =sumMonthDays+day+1;
			System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+sumDays+"天");
		}else{
			int sumDays =sumMonthDays+day;
			System.out.println(year+"年"+month+"月"+day+"日是"+year+"年的第"+sumDays+"天");
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值