首页 > 代码库 > java实现万年历

java实现万年历

敲了一下午,代码比较乱,思路也比较乱,第一次敲这么长的java代码,总结起来就是欠练!!

package homework;

import java.io.*;

import java.lang.*;

import java.util.Scanner;

import java.util.*;

class year{

       year_month_day m=new year_month_day();     

        void set(int y){   

        m.year=y;    m.day=1;  

                           }

        void year_method(){  

                int i=1;   

               for(i=1;i<=12;i++){   

                       System.out.println(+m.year+"年" +i+"月");

                          m.month=i;   m.print();  

                                           }

                               }

                   }  

            class year_month_day{   

                        int itag=0;   

                        int year,month,day;    

                        void set(int y,int m,int d)     {

                           year=y;      month=m;      day=d;     }

             public int  isLeepyear(int year)     {   

                             int m;      

                            if((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0)          

                                        m=1;      

                            else         m=0;  

                                return m;    }      

                void print(){ 

                           itag=isLeepyear(year);   

                          Calendar rili=Calendar.getInstance();    

                          rili.set(year, month-1, 1);   

                          System.out.println("日\t一\t 二\t 三\t 四\t 五\t 六");    

                          int week=rili.get(Calendar.DAY_OF_WEEK)-1;    

                        //System.out.println("week="+week);    

                          String a[]=new String[week+31];    

                          for(int i=0;i     {  

                                     a[i]="**";     }  

                         int num=0;  

                           if(itag==1&&month==2)   

                                        num=29;  

                         else if(itag==0&&month==2)    

                                  num=28;   

                        else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)     

                                     num=31;

                          else num=30;    

                          for(int i=week,n=1;i ++)

                                {     

                                      if(n<9)       a[i]=String.valueOf(n)+" ";   

                                           else       a[i]=String.valueOf(n);      n++;   

                               }   

                           for(int i=0;i ++)    

                                    if(i%7==0){     

                                     System.out.println("");      }  

                               if(a[i]==null)    

                                              a[i]=" ";   

                            System.out.print(a[i]+"\t");     }    

                           System.out.print("\n\n");    }  }

             public class year_calendar {

                               public static void main(String[] args)                                         

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

                                           System.out.println("*         中华万年历                   *");

                                           System.out.println("*       请选择查询方法                 *");   

                                           System.out.println("*1.按年份检索      2.按年月日检索   *");  

                                          System.out.println("*3.退出系统                                        *");  

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

                                          int n;   int year,month,day;   

                                          Scanner s = new Scanner(System.in);  

                                         n=s.nextInt();  

                                          switch(n){  

                                              case 1:         {      

                                                        System.out.println("请输入要检索的年份:");       

                                                        year=s.nextInt();  

                                                        year d=new year();  

                                                         d.set(year);

                                                         d.year_method();         break;           }

                                           case 2:   {    

                                                          System.out.println("请输入要检索的年月日:");    

                                                           year=s.nextInt();    

                                                          month=s.nextInt();    

                                                          day=s.nextInt();  

                                                          year_month_day t=new year_month_day();   

                                                          t.set(year, month, day);   

                                                          t.print();     break;   }  

                                                case 3:break;     

                                                       }  

                                    }

                       }