万年历

打印万年历(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),要求:

(1)编写一个方法判断闰年;

(2)编写一个方法判断某年某月有多少天;

(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;(4)编写一个输出某年某月日历的方法;

(5)编写一个测试方法。

  1 package ssj;
  2 import java.util.Scanner;
  3 public class Data {
  4     
  5     //输入年份
  6 public static int jl1(){
  7      System.out.println("请输入你想要查询的年份:");
  8      Scanner sc = new Scanner(System.in);
  9      
 10      while(true){
 11      int year= sc.nextInt();  
 12      if(year>2900||year<1900){
 13          System.out.println("输入年份不合法,请重新输入!");
 14          return 0;
 15      }
 16      else 
 17      {
 18          return year;
 19      }
 20      }
 21 }
 22 //输入月份
 23 public static int jl2(){
 24 System.out.println("请输入你想要查询该年份的月份:");
 25 Scanner sc = new Scanner(System.in);
 26 int month=sc.nextInt();
 27 while(true){
 28 if(month>12 || month<1)
 29     {  
 30     System.out.println("输入月份不合法,请重新输入!"); 
 31         return 0;
 32     }
 33     else
 34     {
 35         return month;
 36     }
 37  }
 38 }
 39 //主函数
 40 public static void main(String[] args) {
 41         // TODO 自动生成的方法存根
 42     
 43      Scanner sc = new Scanner(System.in);
 44      int sb=0;
 45      int Year=0;
 46      int Month=0;
 47      while(sb==0)
 48      {
 49          System.out.println();
 50          System.out.println("1.输入年份和月份");
 51          System.out.println("2.判断是否是闰年");
 52          System.out.println("3.判断某年某月有多少天");
 53          System.out.println("4.距离1900年1月1日的天数");
 54          System.out.println("5.输出万年历");
 55          int io=sc.nextInt();
 56      switch(io)
 57      {
 58      case 1:Year=jl1();
 59                   while(Year==0)
 60                       {Year=jl1();}
 61                   Month=jl2();
 62                   while(Month==0)
 63                   {Month=jl2();}
 64                   break;
 65               
 66      case 2:
 67              if(LeapYear(Year))
 68              {
 69                  System.out.println("是闰年");continue; 
 70              }
 71              else
 72              {
 73                  System.out.println("不是闰年");continue; 
 74              }
 75      case 3:int days=datamonth(Year,Month);
 76             if(days==0)
 77                 {System.out.println("输入月份不正确!!");continue; 
 78                 
 79                 }
 80                 else
 81                 {
 82                 System.out.println(+Year+"年"+Month+"月有"+days+"天");continue;
 83                 }
 84      case 4:int Z=zdays(Year,Month);
 85              { System.out.println(+Year+"年"+Month+"月前距离1900年1月1日的总天数:"+Z);continue;}
 86      case 5:print(Year,Month);continue;
 87      default:System.out.println("请输入正确选项:");
 88          ;
 89      
 90      }
 91      }
 92     }
 93      //输出万年历
 94       public static void print(int year,int month)
 95       {
 96           int x;
 97           x=zdays(year,month);
 98           int firstDayOfMonth = 0; 
 99           int temp =  x % 7+1 ; //总天数对7取余,
100           if(temp == 7)
101           { 
102               firstDayOfMonth = 0; //週日  
103           }
104           else
105           {  
106               firstDayOfMonth = temp; 
107           } 
108           System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); 
109           for(int k = 0; k < firstDayOfMonth; k++)
110           {
111               System.out.print("\t");  
112           }
113           for(int m = 1; m <= datamonth(year,month); m++)
114           { 
115               System.out.print( m + "\t");  
116           if((x+m) % 7 == 6)
117           {  
118               System.out.print("\n"); 
119               }  
120           } 
121       }  
122       
123      //计算总天数
124       public static int zdays(int year,int month)
125      {
126          int zzdays=0;
127          for(int i=1900;i<year;i++)//计算年的天数
128              if(LeapYear(year))
129              {zzdays+=366;}
130              else
131              {zzdays+=365;}
132          for(int j=1;j<month;j++)//计算月天数
133          {
134              zzdays+=datamonth(year,j);
135          }
136          return zzdays;
137      }
138      //判断闰年
139      public static boolean LeapYear(int year){ //判断闰年  
140             if(year%400==0 || (year%4==0 && year%100!=0)){ //是闰年  
141                 return true;   
142             }  
143               
144             else{  
145                 return false;  
146             }  
147         } 
148      //判断月份天数
149      public static int datamonth(int x,int y){
150          int d=0;
151          switch(y)
152          {
153          case 1:
154          case 3:
155          case 5:
156          case 7:
157          case 8:
158          case 10:
159          case 12:
160              d=31;break;
161          case 2:
162              if(LeapYear(x))
163              { 
164                  d=29;break;
165              }
166              
167              else
168              {
169                  d=28;break;
170              }
171          case 4:
172          case 6:
173          case 9:
174          case 11:
175              d=30;break;
176          default:
177          }
178          return d;
179        
180          
181      }
182     
183 
184 }

 

posted @ 2017-09-23 22:52  小小★书生  阅读(244)  评论(0编辑  收藏  举报