输入年月,输出该月份的日历页

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/The_star_is_at/article/details/70902364

输入年月,输出该月份的日历页

输入输出说明:
输入:
2017 2
输出:
                  1    2    3    4
    5    6    7    8    9   10   11
   12   13   14   15   16   17   18
   19   20   21   22   23   24   25
   26   27   28

import java.util.Calendar;
import java.util.Scanner;
public class Main{
    public static void main(String args[]){
		Calendar cal = Calendar.getInstance();
		Scanner scan = new Scanner(System.in);
		int year,m,days = 0;
		year = scan.nextInt();
		m = scan.nextInt();
		cal.set(year,m-1,1);
		int day = cal.get(Calendar.DAY_OF_WEEK)-1;
		int j = m;
		switch(j){
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
			days=31;
			break;
		case 2:
			if((year%100!=0) && (year%4==0) || (year%400==0))
				days = 29;
			else
				days=28;
			break;
			default:
				days=30;
				break;
		}
		int ii = 1;
		for(;ii<=day;ii++)
			System.out.print("     ");
		for(int k=1;ii<=day+days;ii++){
			System.out.printf("%5d",k++);
			if(ii%7==0)
				System.out.println();
		}
	}
	
}


阅读更多
换一批

没有更多推荐了,返回首页