【笔试】14、判断这一天是这一年的第几天

/**
 * 题目:输入某年某月某日,判断这一天是这一年的第几天?   
 * 时间:2015年7月29日08:31:58
 * 文件:Lianxi14.java
 * 作者:cutter_point
 */
package bishi.zuixin50.t2015729;

import java.util.*;

public class Lianxi14 
{
	public static void main(String [] args)
	{
		//键盘输入年月日
		Scanner sc = new Scanner(System.in);
		int year = sc.nextInt();
		int month = sc.nextInt();
		int day = sc.nextInt();
		getDays(year, month, day);
	}

	public static void getDays(int year, int month, int day)
	{
		//首先我们判断是不是闰年
		int y = year % 4;
		int y2 = year % 400;
		//判断月份,这个是31天的
		List<Integer> m1 = new ArrayList<Integer>(Arrays.asList(1, 3, 5, 7, 8, 10, 12));
		//30天的
		List<Integer> m2 = new ArrayList<Integer>(Arrays.asList(4, 6, 9, 11));

		int days = 0;
		//能被整除是闰年,闰年二月份29天
		for(int i = 1; i <= month; ++i)
		{
			if(m1.contains(i))
			{
				if(i != month)
				{
					days += 31;
				}
				else
				{
					days += day;
				}
			}
			else if(m2.contains(i))
			{
				if(i != month)
				{
					days += 30;
				}
				else
				{
					days += day;
				}
			}
			else
			{
				if(y == 0 || y2 == 0)
				{
					if(i != month)
					{
						days += 29;//二月是29天
					}
					else
					{
						days += day;
					}
				}
				else
				{
					if(i != month)
					{
						//不是闰年
						days += 28;
					}
					else
					{
						days += day;
					}
				}
			}
		}/*for(int i = 1; i <= month; ++i)*/
		System.out.println("这是一年中的第:" + days + "天");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值