丶曼珠沙华 2021-05-28 17:21 采纳率: 100%
浏览 131
已结题

这道Java编程题怎么做

编写程序:已知1901年1月1日为星期二,请编写一个程序,提示用户输入任意年份的数字(包括小于1901年),显示该年份1-12月的完整日历。

 

输入格式:

int year = scanner.nextInt();

int month = scanner.nextInt();

输出格式:

 

输出月头代码:

 

System.out.println("        " + getMonthName(month) + " " + year);

System.out.println("-----------------------------");

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

输出月体代码:

if (i < 10)

System.out.print("   " + i);

else

System.out.print("  " + i);

 

if ((i + startDay) % 7 == 0)

System.out.println();

 

 

输入样例:

2019

1

输出样例:

 

 

        January 2019

-----------------------------

 Sun Mon Tue Wed Thu Fri Sat

           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  31

 

 

输入样例2:

1890

1

输出样例:

 

 

 

        January 1890

-----------------------------

 Sun Mon Tue Wed Thu Fri Sat

               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  31

 

程序模版:

/*提交作业时删除中文注释*/

 

import java.util.Scanner;

public class Main {

       public static void main(String[] args) {

              Scanner scanner =        (1)       ;

              int year = scanner.nextInt();

              int month = scanner.nextInt();

              printMonth(year, month);//调用方法打印日历

       }

 

     //打印日历

              (2)        printMonth(       (3)        ) {

             

              printMonthTitle(year, month);//打印月头:year, month

 

              int startDay = getStartDay(year, month);//获得该月第一天是星期几

 

              int numOfDaysInMonth = getNumOfDaysInMonth(year, month);//获得该月有多少天

 

              printMonthBody(startDay, numOfDaysInMonth);//需要根据第一天是星期几,打印当月天数

       }

 

//获得该月第一天是星期几

              (4)        getStartDay(       (5)       ) {

              int startDay1901 = 2;//已知1901年1月1日为星期二

              if (year >= 1901) {

                     long totalNumOfDays = getTotalNumOfDays(year, month); //获得从1901年1月1日到指定某年某月1日的天数

                     return (int) (      (6)       );

              } else {

                     long totalNumOfDays = getTotalNumOfDaysBefore1901(year, month);//获得从1901年前的某年某月1日到1901年1月1日天数

                     if (totalNumOfDays % 7 <= 2) {

                            return (int) (       (7)        );

                     } else {

                            return (int) (        (8)       );

                     }

              }

 

       }

 

    //获得从1901年1月1日到指定某年某月1日的天数

             (9)        getTotalNumOfDays(       (10)       ) {

 

              long total = 0;

 

              for (int i = 1901; i < year; i++)

                     if (isLeapYear(i))//判断是否为闰年

                            total =       (11)       ;

                     else

                            total =       (12)       ;

 

              for (int i = 1; i < month; i++)

                     total = total + getNumOfDaysInMonth(year, i);//加上指定从1月到 month-1月的天数

 

              return total;

       }

 

    //获得从1901年前的某年某月1日到1901年1月1日天数

               (13)         getTotalNumOfDaysBefore1901(        (14)        ) {

              long total = 0;

 

              for (int i = year + 1; i < 1901; i++)

                     if (isLeapYear(i)) //判断是否为闰年

                            total =        (15)        ;

                     else

                            total =        (16)        ;

 

              for (int i = month; i <= 12; i++)

                     total = total + getNumOfDaysInMonth(year, i);//加上指定从month月到12月31的天数

 

              return total;

       }

 

     //获得某年某月的天数

               (17)         getNumOfDaysInMonth(       (18)        ) {

              if (        (19)        )

                     return 31;

              if (        (20)        )

                     return 30;

              if (        (21)        )

                     if (isLeapYear(year))

                            return 29;

                     else

                            return 28;

              return 0;// 如果月份不正确,返回0

       }

 

//判断是否为闰年,是,返回true

               (22)         boolean isLeapYear(        (23)        ) {

              if (        (24)        )

                     return true;

              return false;

       }

 

//打印月头

               (25)         printMonthTitle(        (26)        ) {

              System.out.println("        " + getMonthName(month) + " " + year);

              System.out.println("-----------------------------");

              System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

       }

 

//获取月的英文全称

               (27)         getMonthName(        (28)        ) {

              String monthName = null;

 

              switch (month) {

              case 1:

                     monthName = "January";

                     break;

              case 2:

                     monthName = "February";

                     break;

              case 3:

                     monthName = "March";

                     break;

              case 4:

                     monthName = "April";

                     break;

              case 5:

                     monthName = "May";

                     break;

              case 6:

                     monthName = "June";

                     break;

              case 7:

                     monthName = "July";

                     break;

              case 8:

                     monthName = "August";

                     break;

              case 9:

                     monthName = "September";

                     break;

              case 10:

                     monthName = "October";

                     break;

              case 11:

                     monthName = "November";

                     break;

              case 12:

                     monthName = "December";

              }

              return monthName;

       }

 

//打印月体

               (29)         printMonthBody(        (30)        ) {

              for (int i = 0; i < startDay; i++)

                     System.out.print("    ");

 

              for (int i = 1; i <= numOfDaysInMonth; i++) {

                     if (i < 10)

                            System.out.print("   " + i);

                     else

                            System.out.print("  " + i);

 

                     if ((i + startDay) % 7 == 0)

                            System.out.println();

              }

              System.out.println();

       }

}

  • 写回答

1条回答 默认 最新

  • Heavenrosaria 2021-10-04 16:28
    关注

    import java.util.Scanner;

    public class Main {

     public static void main(String[] args) {
    
              Scanner scanner = new Scanner(System.in);
    
              int year = scanner.nextInt();
    
              int month = scanner.nextInt();
    
              printMonth(year, month);
       }
    
    
    
     //打印日历
    
     public static void printMonth(int year,int month) {
    
              
    
              printMonthTitle(year, month);
    
    
              int startDay = getStartDay(year, month);
    
    
              int numOfDaysInMonth = getNumOfDaysInMonth(year, month);
    
    
              printMonthBody(startDay, numOfDaysInMonth);
    
       }
    
    
     public static int getStartDay(int year,int month) { 
    
              int startDay1901 = 2;
    
              if (year >= 1901) {
    
                     long totalNumOfDays = getTotalNumOfDays(year, month); 
    
                     return (int) ((startDay1901 + totalNumOfDays) % 7); 
    
              } else {
    
                     long totalNumOfDays = getTotalNumOfDaysBefore1901(year, month);
    
                     if (totalNumOfDays % 7 <= 2) {
    
                            return (int) (startDay1901-totalNumOfDays%7);
    
                     } else {
    
                            return (int) (9-totalNumOfDays%7);
    
                     }
    
              }
    
    
    
       }
    

    public static long getTotalNumOfDays(int year,int month) {

              long total = 0; 
    
              for (int i = 1901; i < year; i++)
    
                     if (isLeapYear(i))
    
                            total = total + 366;
    
                     else
    
                            total = total + 365;
    
    
    
              for (int i = 1; i < month; i++)
    
                     total = total + getNumOfDaysInMonth(year, i);
    
    
    
              return total;
    
       }
    
    
    public static long getTotalNumOfDaysBefore1901(int year,int month) { 
    
              long total = 0;
    
    
    
              for (int i = year + 1; i < 1901; i++)
    
                     if (isLeapYear(i))
                            total = total + 366;
    
                     else
    
                            total = total + 365;
    
    
    
              for (int i = month; i <= 12; i++)
    
                     total = total + getNumOfDaysInMonth(year, i);
    
    
    
              return total;
    
       }
    
      public static int getNumOfDaysInMonth(int year,int i) { 
    
              if (i==1||i==3||i==5||i==7||i==8||i==10||i==12)
    
                     return 31;
    
              if (i==4||i==6||i==9||i==11)
    
                     return 30;
    
              if (i==2)
    
                     if (isLeapYear(year))
    
                            return 29;
    
                     else
    
                            return 28;
    
              return 0;
    
       }
    
      public static boolean isLeapYear(int year) {
    
              if (year % 400 == 0 ||(year % 4 ==0 && year % 100 !=0 ))
    
                     return true;
    
              return false;
    
       }
    
    
    
      public static void   printMonthTitle(int year,int month) { 
    
              System.out.println("        " + getMonthName(month) + " " + year);
    
              System.out.println("-----------------------------");
    
              System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    
       }
    
         public static String getMonthName(int month) { 
    
              String monthName = null;
    
    
    
              switch (month) {
    
              case 1:
    
                     monthName = "January";
    
                     break;
    
              case 2:
    
                     monthName = "February";
    
                     break;
    
              case 3:
    
                     monthName = "March";
    
                     break;
    
              case 4:
    
                     monthName = "April";
    
                     break;
    
              case 5:
    
                     monthName = "May";
    
                     break;
    
              case 6:
    
                     monthName = "June";
    
                     break;
    
              case 7:
    
                     monthName = "July";
    
                     break;
    
              case 8:
    
                     monthName = "August";
    
                     break;
    
              case 9:
    
                     monthName = "September";
    
                     break;
    
              case 10:
    
                     monthName = "October";
    
                     break;
    
              case 11:
    
                     monthName = "November";
    
                     break;
    
              case 12:
    
                     monthName = "December";
    
              }
    
              return monthName;
    
       }
    
      public static void printMonthBody(int startDay,int numOfDaysInMonth) { 
    
              for (int i = 0; i < startDay; i++)
    
                     System.out.print("    ");
    
    
    
              for (int i = 1; i <= numOfDaysInMonth; i++) {
    
                     if (i < 10)
    
                            System.out.print("   " + i);
    
                     else
    
                            System.out.print("  " + i);
    
    
    
                     if ((i + startDay) % 7 == 0)
    
                            System.out.println();
    
              }
    
              System.out.println();
    
       }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题