import java.util.Scanner;
class rili
{
public static void main(String[] args)
{
boolean flag1=true;
while (flag1)
{
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份");
int year=sc.nextInt();
if (year<1900 || year>2900)
{
System.out.println("超出查询范围");
return;
}
System.out.println("请输入月份");
int month=sc.nextInt();
if (month>12 || month<=0)
{
System.out.println("输入错误");
return;
}
int sum=0;
//累计年的天数
sum=yearSum(year,sum);
//累计月的天数
sum=monthSum(year,month,sum);
sum+=1;
//获取属相
getZoidac(year);
//获取当月天数
int day=getDay(year,month);
//打印日历
printRili(sum,day);
System.out.println("\r\n输入1继续,输入0结束");
int flag=sc.nextInt();
if(flag==1)
{
flag1=true;
}
else
{
flag1=false;
}
}
}
//累计月年的天数
public static int yearSum(int year,int sum)
{
for (int i=1900;i<year ;i++ )
{
if ((i%4==0 && i%100!=0) || i%400==0)
{
sum+=366;
}
else
{
sum+=365;
}
}
return sum;
}
//累计月的天数
public static int monthSum(int year,int month,int sum)
{
for (int j=1;j<month ;j++ )
{
if (j==2)
{
if ((year%4==0 && year%100!=0) || year%400==0)
{
sum+=29;
}
else
{
sum+=28;
}
}
else if (j==4 || j==6 || j==9 || j==11)
{
sum+=30;
}
else
{
sum+=31;
}
}
return sum;
}
//获取当月天数
public static int getDay(int year,int month)
{
int day;
if (month==2)
{
if (year%4==0 && year%100!=0 || year%400==0)
{
day=29;
}
else
{
day=28;
}
}
else if (month==4 || month==6 || month==9 || month==11)
{
day=30;
}
else
{
day=31;
}
return day;
}
//打印日历
public static void printRili(int sum,int day)
{
int weekday=sum%7;
System.out.println("日\t一\t二\t三\t四\t五\t六\t");
int count=1;
for (int i=1;i<=weekday ;i++ )
{
System.out.print("\t");
count++;
}
for (int i=1;i<=day ;i++ )
{
if (count%7==0)
{
System.out.println(i);
count=1;
}
else
{
System.out.print(i+"\t");
count++;
}
}
}
public static void getZoidac(int year)
{
int zodiac=(year-1900+1)%12;
switch(zodiac)
{
case 1:
System.out.println("今年是鼠年");
break;
case 2:
System.out.println("今年是牛年");
break;
case 3:
System.out.println("今年是虎年");
break;
case 4:
System.out.println("今年是兔年");
break;
case 5:
System.out.println("今年是龙年");
break;
case 6:
System.out.println("今年是蛇年");
break;
case 7:
System.out.println("今年是马年");
break;
case 8:
System.out.println("今年是羊年");
break;
case 9:
System.out.println("今年是猴年");
break;
case 10:
System.out.println("今年是鸡年");
break;
case 11:
System.out.println("今年是狗年");
break;
case 0:
System.out.println("今年是猪年");
break;
}
}
}
本次代码为基础代码 没有选择用数组 所以冗余大
12-03