zoukankan      html  css  js  c++  java
  • 万年历---java版

      程序难点 :

        1. 每年每个月有多少天?

        2. 每个月的1号是星期几?

        3. 每年的2月份是多少天?

      难点解析 :

        1. 每年每个月除去1 3 5 7 8 10 12是31天以外, 其他月份(除去2月)都是30天.

        2. 根据java提供的Calendar的DAY_OF_WEEK来获取. c.get(Calendar.DAY_OF_WEEK);

          注意, 在国外每周的第一天是周日,所以它的对应关系为

          1  2  3  4  5  6  7  

          日 一 二 三 四 五 六

        3. 平年28天, 闰年29天.

          注意 : 闰年是可以被4整除或者能被100整除也可被400整除, 平年是能被100整除而不能被400整除.

    JAVA 代码 : 

      1 /**
      2      * 31天的月份 
      3      */
      4     private static final List<Integer> singleMonList = new ArrayList<Integer>();
      5     
      6     static{
      7         singleMonList.add(0);
      8         singleMonList.add(2);
      9         singleMonList.add(4);
     10         singleMonList.add(6);
     11         singleMonList.add(7);
     12         singleMonList.add(9);
     13         singleMonList.add(11);
     14     }
     15     
     16     public static void calendarYear(int year){
     17             Calendar c = Calendar.getInstance();
     18             c.set(Calendar.YEAR, year);
     19             System.out.println("-----------------------------"+year+"年start------------------------------");
     20             for (int i = 0; i < 12; i++) {
     21                 System.out.println();
     22                 System.out.println(year + "年" + (i + 1) + "月");
     23                 System.out.println();
     24                 System.out.println("日	" + "一	" + "二	" + "三	" + "四	"+ "五	" + "六");
     25                 c.set(Calendar.MONTH, i);
     26                 c.set(Calendar.DATE, 1);
     27                 int week = c.get(Calendar.DAY_OF_WEEK);
     28                 int weekTemp = week - 1;
     29                 int days = getMonthOfDays(year, i); // 获取天数
     30                 // 天数打印
     31                 for (int j = 1; j <= days; j++) {
     32                     if (j == 1){
     33                         getBlank(weekTemp); // 打印空格
     34                     }
     35                     if (weekTemp == 7) {    //换行
     36                         System.out.println();
     37                         if (j < 10) {
     38                             System.out.print(" " + j + "	");
     39                         } else {
     40                             System.out.print(j + "	");
     41                         }
     42                         weekTemp = 1;
     43                     } else {
     44                         if (j < 10) {
     45                             System.out.print(" " + j + "	");
     46                         } else {
     47                             System.out.print(j + "	");
     48                         }
     49                         weekTemp++;
     50                     }
     51                 }
     52                 System.out.println();
     53                 System.out.println();
     54                 System.out.println();
     55             }
     56             System.out.println("-----------------------------"+year+"年end------------------------------");
     57     }
     58 
     59     private static void getBlank(int blankNum) {
     60         for (int i = 0; i < blankNum; i++) {
     61             System.out.print(" 	");
     62         }
     63     }
     64 
     65     private static int getMonthOfDays(int year, int month) {
     66         int days = 0;
     67         if (singleMonList.contains(month)) {
     68             days = 31;
     69         } else {
     70             if (month == 1) {
     71                 if (((year % 100 != 0) && (year % 4 == 0))
     72                         || ((year % 100 == 0) && (year % 400 == 0))) {
     73                     days = 29;
     74                 } else {
     75                     days = 28;
     76                 }
     77             } else {
     78                 days = 30;
     79             }
     80         }
     81         return days;
     82     }
     83     
     84     private static boolean checkYear(int year){
     85         if(year>Long.MAX_VALUE){
     86             return false;
     87         }
     88         if(year < Long.MIN_VALUE){
     89             return false;
     90         }
     91         return true;
     92     }
     93     
     94     @SuppressWarnings("resource")
     95     public static void main(String[] args) throws Exception {
     96         
     97         while(true){
     98             System.out.print("请输入年份 (1: 退出程序): ");
     99             Scanner sc = new Scanner(System.in);
    100             Integer year = sc.nextInt();
    101             if(!checkYear(year)) {
    102                 continue;
    103             }
    104             if(year==1) System.exit(0);
    105             calendarYear(year);
    106         }
    107     }

      注 : 基本上解决了上述三个难点, 这个程序就可以迎刃而解. 其他的就是一些显示上的排版.   

  • 相关阅读:
    UOS ROOT如何SSH登陆
    UOS打印日志提示Can’t create temporary file,无法打印如何处理
    UOS简易OEM ISO镜像的步骤(UOS如何自行定制镜像文件)
    UOS火狐浏览器如何下载并安装Adobe Flash Player插件【AMD】
    UOS如何录制屏幕视频
    UOS怎么进入到单用户模式
    UOS免密访问windows共享文件夹
    UOS命令行服务器离线授权码激活步骤
    UOS怎么安装搜狗拼音输入法【x86】
    UOS如何安装RTX客户端-更新版(X86)
  • 原文地址:https://www.cnblogs.com/duwenlei/p/5050216.html
Copyright © 2011-2022 走看看