输入年月日判断是该年的第几天

题目:
   输入年月日判断是该年的第几天
思路:

  • 首先判断该年是否是闰年
  • 通过case判断大小月进行累加

实现:

import java.util.Scanner;

public class Calendar {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner reader = new Scanner(System.in);
		int year = reader.nextInt();
		int month = reader.nextInt();
		int day = reader.nextInt();
		int alldays=0;
		int flag;
		if((year/4==0&&year/100!=0)||(year/400==0)){
			flag = 28;
		}else{
			flag = 29;
		}
/*		方法一:
		switch(month)
		{
			case 1:alldays = day;break;
			case 2:alldays = 31+day;break;
			case 3:alldays = 31+flag+day;break;
			case 4:alldays = 31+flag+31+day;break;
			case 5:alldays = 31+flag+31+30+day;break;
			case 6:alldays = 31+flag+31+30+31+day;break;
			case 7:alldays = 31+flag+31+30+31+30+day;break;
			case 8:alldays = 31+flag+31+30+31+30+31+day;break;
			case 9:alldays = 31+flag+31+30+31+30+31+31+day;break;
			case 10:alldays = 31+flag+31+30+31+30+31+31+30+day;break;
			case 11:alldays = 31+flag+31+30+31+30+31+31+30+31+day;break;
			case 12:alldays = 31+flag+31+30+31+30+31+31+30+31+30+day;break;
		}
*/
		//方法二:
		switch(month-1)
		{
			case 11:alldays = alldays+30;
			case 10:alldays = alldays+31;
			case 9:alldays = alldays+30;
			case 8:alldays = alldays+31;
			case 7:alldays = alldays+31;
			case 6:alldays = alldays+30;
			case 5:alldays = alldays+31;
			case 4:alldays = alldays+30;
			case 3:alldays = alldays+31;
			case 2:alldays = alldays+flag;
			case 1:alldays = day+31;	
		}
		System.out.println(alldays);
	}
}

你可能感兴趣的:(#,小题目)