编程输出2008年日历。日历中要求含有月份,日期,星期(如星期一),然后统计并输出2008年日期的个位数的相应的星期恰好相同的总天数,例如2008年9月1日恰好是星期一
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package l_calendar;
/**
*
* @author lonely
*/
public class L_Calendar {
/**
* 输出2008年日历
* 统计日期的各位数与相应的星期恰好相同的总天数
*/
public static int YEAR=2008;//2008年
public static void sq_Calendar()
{
int month=1;//月份
int i=0;//循环变量
int n=0;//当月的日期数
int n1;//输出日期时的换行操作
int sum=0;//统计总天数
int d=0;//计算每个月的第一天是星期几
int days=1;//统计每个月的一号是一年中的第几天
int []days_m1={31,28,31,30,31,30,31,31,30,31,30,31};//非闰年每个月的天数
int []days_m2={31,29,31,30,31,30,31,31,30,31,30,31};//闰年每个月的天数
while(month<=12)
{
n1=n=0;
days=1;
//判断月份
if(month==1||month==3||month==5||month==7
||month==8||month==10||month==12)
n=31;
else if(month!=2)
n=30;
if (YEAR%4==0&&YEAR%100!=0||YEAR%400==0)//判断闰年
{
if (month==2)
n=29;
for(i=0;i<month-1;i++)
days=days+days_m2[i];//统计每个月的一号是一年中的第几天
}
else
{
if (month==2)
n=31;
for(i=0;i<month-1;i++)
days=days+days_m1[i];//统计每个月的一号是一年中的第几天
}//判断月份结束
d=(YEAR-1+(YEAR-1)/4-(YEAR-1)/100+(YEAR-1)/400+days)%7;
//计算每个月的第一天是星期几
System.out.println();
//月份输出操作
System.out.println(" "+month+"月");
System.out.println("Sum"+'\t'+"MON"+'\t'+"TUE"+'\t'+"WEN"+
'\t'+"THU"+'\t'+"FRI"+'\t'+"SAT");
for(i=0;i<d;i++)
System.out.print('\t');
for (i=0;i<7-d;i++)
{ System.out.print(i+1);
System.out.print('\t');
}
System.out.println();
for(i=7-d;i<n;i++)
{
System.out.print(i+1);
System.out.print('\t');
n1=n1+1;
if(n1%7==0)
System.out.println();
}
System.out.println();
//统计每个月的各位数与相应的星期恰好相同的天数
switch(d)
{
case 1:
System.out.println(month
+"月1号-7号的各位数与相应的星期恰好相同");
sum=sum+7;break;
case 5:
System.out.println(month
+"月11号-17号的各位数与相应的星期恰好相同");
sum=sum+7;break;
case 2:
System.out.println(month
+"月21号-27号的各位数与相应的星期恰好相同");
sum=sum+7;break;
case 6:
if(month==1||month==3||month==5||month==7
||month==8||month==10||month==12)
{System.out.println(month
+"月31号的各位数与相应的星期恰好相同");
sum=sum+1;}
break;
}
month++;
}
System.out.println(YEAR
+"年日期的各位数与相应的星期恰好相同的总天数为"
+sum);
}
public static void main(String[] args) {
// TODO code application logic here
sq_Calendar();
}//main结束
}//L_Calendar结束