用Java语言写的万年历的小程序

import java.util.*;//万年历
public class Test12
{
        public static void main(String args[])
        {
                System.out.println("请输入年份和月份");
                Scanner scan=new Scanner(System.in);
            int year =scan.nextInt();
                int month=scan.nextInt();
                int sum=0;
                int day;
            int day2;
       
                        if((year%4==0)&&(year%100!=0)||(year%400==0))
                        {   
                                if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
                                {
                                        day2=31;
                                }
                                else if(month==2)
                                {
                                        day2=29;
                                }
                                else
                                        {
                                                day2=30;
                                    }
                                for(int i=1;i<month;i++)
                            {
                                        if((i==1)&&(i==3)&&(i==5)&&(i==7)&&(i==8)&&(i==10)&&(i==12))
                                        {
                                                day=31;
                                        }
                                        else if(i==2)
                                        {
                                                day=29;
                                        }
                                        else
                                           {
                                                 day=30;
                                           }
                                         sum=sum+day;
                                  }       

                            }
                                else
                            {  
                                   if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
                                   {
                                          day2=31;
                                    }
                                   else if(month==2)
                                     {
                                           day2=28;
                                     }
                                   else
                                          {
                                                day2=30;
                                      }
                                       
                                        for(int i=1;i<month;i++)
                                        {
                                          if((i==1)&&(i==3)&&(i==5)&&(i==7)&&(i==8)&&(i==10)&&(i==12))
                                           {
                                                  day=31;
                                            }
                                        else if(i==2)
                                           {
                                                  day=28;
                                           }
                                        else
                                           {
                                                 day=30;
                                           }
                                         sum=sum+day;
                                        }
                         }

                                 sum=sum+(year-1900)*365;
                                  int y=1900;
                                  int k=0;
                                        while(y<year)
                                  {
                                                if((y%4==0)&&(y%100!=0)||(y%400==0))
                                                  {
                                                        k++;
                                                  }
                                                  y++;

                                          }
                                          sum=sum+y;
                   System.out.println("上个月月底距离1900年1月一号的总天数为"+sum);
                     
                              int m=sum%7;
                                  System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天\t");
                          for(int n=1;n<=m;n++)
                             {
                                            System.out.print(" \t");
                                     }
                               
                                         for(int t=1;t<=day2;t++)
                             {
                                                 System.out.print(t+"\t");
                                                   if((m+t)%7==0)
                                                   {
                                                           System.out.println();
                                                   }
                                         }

        }

}

GitHub:  https://github.com/HuaDanJson

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的Java万年历程序的示例代码: ```java import java.util.Scanner; public class CalendarProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入年份:"); int year = scanner.nextInt(); System.out.print("请输入份:"); int month = scanner.nextInt(); printCalendar(year, month); } public static void printCalendar(int year, int month) { // 计算该的第一天是星期几 int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印日历头部 System.out.println("日\t一\t二\t三\t四\t五\t六"); // 打印前面的空格 for (int i = 0; i < firstDayOfWeek; i++) { System.out.print("\t"); } // 打印日期 int daysInMonth = getDaysInMonth(year, month); for (int day = 1; day <= daysInMonth; day++) { System.out.print(day + "\t"); if ((day + firstDayOfWeek) % 7 == 0) { System.out.println(); } } } public static int getFirstDayOfWeek(int year, int month) { // 使用Zeller公式计算给定年份和份的第一天是星期几 if (month < 3) { month += 12; year--; } int century = year / 100; int yearOfCentury = year % 100; int weekDay = (yearOfCentury + yearOfCentury / 4 + century / 4 - 2 * century + 26 * (month + 1) / 10) % 7; // 将周日(0)转换为周一(1)到周六(6) return (weekDay + 7) % 7 + 1; } public static int getDaysInMonth(int year, int month) { // 计算给定年份和份的天数 int daysInMonth; // 判断是否为闰年 if (month == 2 && isLeapYear(year)) { daysInMonth = 29; } else { switch (month) { case 4: case 6: case 9: case 11: daysInMonth = 30; break; default: daysInMonth = 31; } } return daysInMonth; } public static boolean isLeapYear(int year) { // 判断是否为闰年 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } } ``` 这个程序接受用户输入的年份和份,并打印出对应的万年历。它使用了Zeller公式来计算给定年份和份的第一天是星期几,然后根据每个的天数打印出日历。程序还包含了一个`isLeapYear`方法来判断是否为闰年,以及`getDaysInMonth`方法来获取给定年份和份的天数。 您可以运行这个程序并输入年份和份来查看对应的万年历。希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张海龙_China

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值