当前位置: 代码迷 >> Java相关 >> 【Java自学】 打印台历信息
  详细解决方案

【Java自学】 打印台历信息

热度:47   发布时间:2016-04-22 19:26:14.0
【Java自学】 打印日历信息
  1 package codeTask_FangFa;  2 //                                                                            5.34  使用zeller公式,打印某年某月的日历信息。  3 import java.util.Scanner;  4 public class PrintRiLi {  5        public static void main(String[] args){  6            Scanner input = new Scanner(System.in);  7            System.out.println("请输入需要打印日历的年份:");  8            int year = input.nextInt();  9            System.out.println("请输入需要打印日历的月份:"); 10            int month = input.nextInt(); 11            printTitle(year,month); 12            printBody(year,month); 13             14        } 15         16        public static void printTitle(int year,int month){                           //第二战场--头 17            System.out.println("                                                      日             历");    18            System.out.println("                            "+getMonth(month)+"                                "+year); 19            System.out.println("        星期日     星期一    星期二    星期三    星期四    星期五    星期六");  20            System.out.println("--------------------------------------------------"); 21        } 22         23        public static String getMonth(int month){                            // 这就不说啥了 24  25            switch(month){ 26            case 1:return "一月"; 27            case 2:return "二月"; 28            case 3:return "三月"; 29            case 4:return "四月"; 30            case 5:return "五月"; 31            case 6:return "六月"; 32            case 7:return "七月"; 33            case 8:return "八月"; 34            case 9:return "九月"; 35            case 10:return "十月"; 36            case 11:return "十一月"; 37            case 12:return "十二月"; 38            } 39            System.out.println("输入的月份出错,程序退出!"); 40            System.exit(0); 41            return "-1"; 42        } 43         44        public static void printBody(int year,int month){                                                     //主战场--可要看仔细了。                     45            int count = getXingQi(year,month);                     46            int nim = getNumberInMonth(year,month);                           // nim  全拼是 number in month. 47            int day = 1;                                                             //每月的日子,从 1 开始。 48            switch (count){ 49            case 0 : count = 1;break; 50            case 1 : count = 2;break; 51            case 2 : count = 3;break; 52            case 3 : count = 4;break; 53            case 4 : count = 5;break; 54            case 5 : count = 6;break; 55            case 6 : count = 7;break; 56            } 57            for(int i =1;i<count;i++){ 58                 System.out.printf("%7s"," "); 59             } 60             while(day<nim){ 61  62                 while(count%7!=0){ 63                     System.out.printf("%7d",day); 64                     if(day==nim) 65                         System.exit(0); 66                     count++; 67                     day++; 68                      69                 } 70                 System.out.printf("%7d\n",day); 71                 count++; 72                 day++; 73  74             } 75  76            } 77  78         79        public static int getXingQi(int year,int month){                                                       //得到某月1号是星期几。 这里会用到zeller公式。 80            int m = month; 81            if(month == 1){ 82                m = 13; 83                year--; 84            } 85            else if(month ==2){ 86                m = 14; 87                year--; 88            } 89            int d = 1; 90            int c = year/100; 91            int y = year%100; 92            int w1 =  y+y/4+c/4-2*c+26*(m+1)/10+d-1; 93            int w = (w1%7+7)%7; 94            return w ; 95        } 96         97        public static int getNumberInMonth(int year,int month){                                //返回某月共有几天。 98            if(isRunNian(year)){ 99                switch (month){100                case 1 : return 31;101                case 2 : return  29  ;102                case 3 : return  31  ;103                case 4 : return  30  ;104                case 5 : return  31  ;105                case 6 : return  30  ;106                case 7 : return  31  ;107                case 8 : return  31  ;108                case 9 : return  30  ;109                case 10 : return  31  ;110                case 11: return   30 ;111                case 12 : return  31  ;112                }113            }114            else{115                switch (month){116                case 1 : return 31;117                case 2 : return  28  ;118                case 3 : return  31  ;119                case 4 : return  30  ;120                case 5 : return  31  ;121                case 6 : return  30  ;122                case 7 : return  31  ;123                case 8 : return  31  ;124                case 9 : return  30  ;125                case 10 : return  31  ;126                case 11: return   30 ;127                case 12 : return  31  ;128                }129            }130            return -1;                                                              //错误情况下的返回值。131        }132        133        public static boolean isRunNian(int year){                                    //判断闰年。是就true134            if(year%400==0||(year%4==0&year%100!=0))135                return true;136            return false;137        }138 }

 

  相关解决方案