一个简单的万年历

今天第一次尝试做了一个简单的万年历

不过是在老师的提醒之下完成的

需要学习的还有很多,菜鸟之路漫漫。

心得体会:

做稍微要绕一点头脑的东西,

必须思路清晰,

思路真的重要。

开始之前一定理清楚思路,如果像我这么笨的就把思路写在本子上,一小步一小步的接近最终的目的。

最好在定义方法的时候 旁边就打上中文字

方便自己发现错误也方便自己理解  不能当时明白那段代码是什么意思 过后就不理解 或者理解很慢

 思路:

万年历
a:先输出提示语句,并接受用户输入的年、月。
b:根据用户输入的年,先判断是否是闰年。(闰年2月有29天,平年2月有28天);
C:根据用户输入的月来判断月的天数。
D:用循环计算用户输入的年份距1900年1月1日(星期一)的总天数。* yearday
E:用循环计算用户输入的月份距输入的年份的1月1日共有多少天。*
F:相加D与E的天数,得到总天数。
G:用总天数来计算输入月的第一天的星期数。
H:根据G的值,格式化输出这个月的日历!*
H提示:
1.如果当月1日如果不是周日先打印第一行,再打印后面的
(1)startDay=0~5,先打印若干个空格,startDay==0时,是星期一,应该在星期日处打印一个空格,以此类推[0~startDay+1)
(2)再打印第一行的日期[1,7-startDay)
(3)打印换行
(4)第一行打印的天数为:1~(6-startDay),所以第二行从7-startDay开始打印,打印到当月总天数为止,每7个换行
2.如果startDay是6(周日),直接从1日开始打印到最后一天,每7个换行

提示:当月1日如果不是周日,应循环输出\t以便对齐
当月1日的星期数加上日期的号,如果能被7整除,则应换行.

import java.util.Scanner;

class test2 
{
public static void main(String[] args) 
{

Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt();
System.out.println("请输入月份");
int month = sc.nextInt();
int sumday = 0;
int yeardays = allYeardays(year);
    int monthdays = allMonthdays(month,year);
        sumday = yeardays + monthdays;
    int firstDayOfMonth = 0;
    int temp = 1+sumday%7; //用总天数来计算输入月的第一天的星期数  +1是这个月第一天
    if (temp ==7)
   {
   firstDayOfMonth = 0;
   }
   else
{
   firstDayOfMonth = temp;
   }
       System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");// 先打印表头
        for(int k = 1; k <=firstDayOfMonth; k++)
        {
       System.out.print("\t");// 打印空格数
        }
       for(int m = 1; m <= days(month,year); m++)
   {
       System.out.print( m + "\t");// 打印日期按星期的排布
       if((temp + m) % 7 == 0)      // 七天之后 换行另起
   {
       System.out.print("\n");
           }
       }
}
public static int allYeardays(int year)          //用循环计算用户输入的年份距1900年1月1日(星期一)的总天数。

int yearday = 0;
for (int a = 1900;a<year ;a++ )
{
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
{
yearday = yearday + 366;         
}
else
{
yearday = yearday + 365;
}
}
return yearday;
 


}
public static boolean run(int year)      //根据用户输入的年,判断是否是闰年。
{
boolean isRun = false;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{

            isRun = true;
}
return isRun;


}
public static int allMonthdays(int month,int year)    //用循环计算用户输入的月份距输入的年份的1月1日共有多少天。
{
int monthday = 0;
int day = 0;
       for (int j = 1; j<=month;j++ )
 {
              switch(j)
 {
  case 1:
                  case 3:
                  case 5:
                  case 7:
                  case 8:
                  case 10:
                  case 12:
                  day = 31;
                  break;
  case 4:
  case 6:
  case 9:
  case 11:
  day = 30;
  break;
  case 2:
  if (run(year))
  {
     day = 29;
  break;
  }
  else
      {
  day = 28;
      break;
  }
  default :
   System.out.println("输入月份不正确!");
  }
    if(j < month)
         {
                  monthday = monthday + day;
             }
 }
 return monthday;
         

}
 public static int days(int month,int year)        //根据用户输入的年份 月份 返回该月的天数
{
  int day = 0;
       for (int j = 1; j<=month;j++ )
 {
              switch(j)
 {
  case 1:
                  case 3:
                  case 5:
                  case 7:
                  case 8:
                  case 10:
                  case 12:
                  day = 31;
                  break;
  case 4:
  case 6:
  case 9:
  case 11:
  day = 30;
  break;
  case 2:
  if (run(year))
  {
     day = 29;
  break;
  }
  else
      {
  day = 28;
      break;
  }
  default :
   System.out.println("输入月份不正确!");
  }
 }
 return day;
        
 
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是用C语言编写的一个简单万年历程序: ```c #include <stdio.h> int days_of_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char week[7][4] = {"日", "一", "二", "三", "四", "五", "六"}; int is_leap_year(int year) { return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)); } int get_days_of_month(int year, int month) { if (month == 2 && is_leap_year(year)) { return 29; } else { return days_of_month[month]; } } int main() { int year, month; printf("请输入年份: "); scanf("%d", &year); printf("请输入月份: "); scanf("%d", &month); printf("====================\n"); printf(" %d 年 %d 月\n", year, month); printf("====================\n"); printf("日 一 二 三 四 五 六\n"); // 获得当月1号是星期几 int first_day_of_month = 1; for (int i = 1900; i < year; i++) { first_day_of_month += (is_leap_year(i) ? 366 : 365); } for (int i = 1; i < month; i++) { first_day_of_month += get_days_of_month(year, i); } first_day_of_month = first_day_of_month % 7; for (int i = 0; i < first_day_of_month; i++) { printf(" "); } // 输出当月所有天数 int days_of_current_month = get_days_of_month(year, month); for (int i = 1; i <= days_of_current_month; i++) { printf("%2d ", i); if ((first_day_of_month + i) % 7 == 0) { printf("\n"); } } if ((first_day_of_month + days_of_current_month) % 7 != 0) { printf("\n"); } printf("====================\n"); return 0; } ``` 此程序可以将输入的年份和月份作为参数,输出指定年月的日历,例如: ``` 请输入年份: 2022 请输入月份: 11 ==================== 2022 年 11 月 ==================== 日 一 二 三 四 五 六 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 29 30 ==================== ``` ### 回答2: 编写一个万年历的C程序涉及到以下几个步骤: 1. 定义函数来判断某一年是否为闰年。闰年的判断规则是: - 如果该年份能被4整除但不能被100整除,则是闰年。 - 如果该年份能被400整除,则也是闰年。 2. 定义函数来计算某一年1月1日是星期几。我们可以利用Zeller公式来计算,该公式的逻辑是: - 根据公式: h = (q + [(13 * (m + 1)) / 5] + K + [K / 4] + [J / 4] - 2 * J) mod 7 - q 是日期,m 是月份(3代表3月,4代表4月,...,在公式中1月和2月被视为前一年的13月和14月),y 是年份的前两位数字,K 是年份的后两位数字,J 是年份的前两位数字。 - h 的值表示当年1月1日的星期几,0代表星期日,1代表星期一,以此类推。 3. 在主函数中,首先获取用户输入的年份,并使用闰年判断函数来确定该年份是否为闰年。然后使用星期计算函数来计算该年份的1月1日是星期几。 4. 根据1月1日的星期几,构建一个二维数组来表示整个年份的日历。数组的行表示星期,从0到6对应于星期日到星期六,列表示日期的排列。 - 我们可以使用循环来填充数组,首先填充1月,然后是2月,以此类推,直到12月。在填充的过程中,需要根据每个月的天数以及1月1日是星期几来确定日期的排列。 5. 最后,打印输出整个年份的万年历。 注意:以上步骤只是程序的基本框架,具体的实现细节还需根据个人的编程能力和经验来完善。 ### 回答3: 编写一个万年历的程序需要考虑到年、月、日的变化和计算规则。以C语言为例,可以使用基本的控制流和算术运算来实现。以下是一个简单的实现: ``` #include <stdio.h> int isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return 1; // 是闰年 } else { return 0; // 不是闰年 } } int main() { int month, year; printf("请输入年份:"); scanf("%d", &year); printf("请输入月份:"); scanf("%d", &month); int daysInMonth = 31; if (month == 2) { if (isLeapYear(year)) { daysInMonth = 29; } else { daysInMonth = 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { daysInMonth = 30; } printf("\n------------------------\n"); printf(" %d年%d月\n", year, month); printf("------------------------\n"); printf("日 一 二 三 四 五 六\n"); int dayOfWeek = 0; // 0表示星期日 // 计算并打印每天的日期 for (int day = 1; day <= daysInMonth; day++) { if (day == 1) { // 根据1号是星期几,确定第一行空格的数量 for (int i = 0; i < dayOfWeek; i++) { printf(" "); } } printf("%2d ", day); // 每7天换行 if ((dayOfWeek + day) % 7 == 6) { printf("\n"); } dayOfWeek = (dayOfWeek + 1) % 7; } printf("\n------------------------\n"); return 0; } ``` 这个程序首先判断输入的年份是否为闰年,然后根据月份确定该月的天数。接着,使用一个循环打印出该月的日历,根据每个月的1号是星期几对齐打印内容。最后,根据每7天换行。这样可以得到一个简单万年历。 注:以上代码只是一个范例,可能没有考虑一些特殊情况,如输入的年份和月份是否合法等。实际使用中可以根据自己的需求和补充完善相关功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值