打印万年历(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),要求:
(1)编写一个方法判断闰年;
(2)编写一个方法判断某年某月有多少天;
(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;(4)编写一个输出某年某月日历的方法;
(5)编写一个测试方法。
import java.util.*;
public class yearlist {
public static void main(String[] args) {
// TODO Auto-generated method stub
paly();
}
private static void paly() {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println("1...判断闰年");
System.out.println("2...判断某年某月有多少天");
System.out.println("3...某年某月前距离1900年1月1日的总天数");
System.out.println("4...输出某年某月日历");
System.out.println("5...退出");
int m;
m=in.nextInt();
switch(m)
{
case 1:
int text;
System.out.println("请输入年份...");
int year=in.nextInt();
text=testyear(year);
if(text==1)
System.out.println("yes");
else
System.out.println("no");
break;
case 2:
int text1;
System.out.println("请输入年...");
int year1=in.nextInt();
System.out.println("请输入月...");
int month1=in.nextInt();
text1=testmonth(year1,month1);
System.out.println(text1);
break;
case 3:
int text2;
System.out.println("请输入年...");
int year2=in.nextInt();
System.out.println("请输入月...");
int month2=in.nextInt();
text2=calculate(year2,month2-1);
System.out.println(text2);
break;
case 4:
System.out.println("请输入年...");
int year3=in.nextInt();
System.out.println("请输入月...");
int month3=in.nextInt();
desplay(year3,month3);
break;
case 5:
System.exit(0);
break;
}
}
}
private static void desplay(int year,int month) {//万年历界面
// TODO Auto-generated method stub
int i,j,k=1;
int day=calculate(year,month-1);
int count,monday;
count=testmonth(year,month);
monday=day%7+1;
System.out.println("一 二 三 四 五 六 日");
for(i=1;i<=monday-1;i++)System.out.print(" ");
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
System.out.print(k+" ");
if(8-monday==k||k==count)
{k++;
break;
}
k++;
}
System.out.println("");
if(k>=count) break;
}
}
private static int calculate(int year,int month) {//计算距离1900.01.01天数
int i,j,count=0;
for(i=1900;i<=year;i++)
{
for(j=1;j<=12;j++)
{
count=count+testmonth(i,j);
if(i==year&&j==month)
break;
}
}
return count;
}
private static int testmonth(int year,int month) {//计算当月天数
// TODO Auto-generated method stub
int x,y=0;
x=testyear(year);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
y=31;
break;
case 2:
if(x==1)
y=29;
else
y=28;
break;
case 4:
case 6:
case 9:
case 11:
y=30;
break;
}
return y;
}
public static int testyear(int m) {//判断闰年
// TODO Auto-generated method stub
if(m%4==0&&m%100!=0||m%400==0)
return 1;
else
return 0;
}
}
原文:http://www.cnblogs.com/wgx521/p/7577492.html