由java编写的万年历

由java编写的万年历

  1. package perpetualCalendar;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class Part01 {  
  6.     public static void main(String[] args) {  
  7.         Scanner scanner = new Scanner(System.in);  
  8.         System.out.println("请输入年:");  
  9.         int year = scanner.nextInt();  
  10.         System.out.println("请输入月:");  
  11.         int month = scanner.nextInt();  
  12.   
  13.         //计算输入月份有几天  
  14.         int monthDay = countMonthDay(year,month);  
  15.           
  16.         //距离1900年1月1日的总天数  
  17.         int day;  
  18.         day = countDay(year);  
  19.           
  20.         //输入月份距离输入年份的1月1日的总天数  
  21.         int day2;  
  22.         day2 = countCurrentYear(year, month);  
  23.           
  24.         //总天数  
  25.         int totalDay;  
  26.         totalDay = day + day2;  
  27.           
  28.         //输入月的第一的星期数  
  29.         int week;  
  30.         week = (totalDay + 1)%7;  
  31.           
  32.         System.out.print("星期日"+'\t'+"星期一"+'\t'+"星期二"+'\t'+"星期三"+'\t'+"星期四"+'\t'  
  33.                 +"星期五"+'\t'+"星期六"+'\n');  
  34.         for(int i=0;i<week;i++)  
  35.             System.out.print('\t');  
  36.         for(int i = 1;i<=monthDay;i++) {  
  37.             if(week%7 == 0)  
  38.                 System.out.println();  
  39.             System.out.print(i);  
  40.             System.out.print('\t');  
  41.             week ++;  
  42.         }  
  43.           
  44.     }  
  45.       
  46.     //判断是否为闰年  
  47.     static boolean testLeapYear(int year) {  
  48.         if((year%4 == 0&&year%100 == 0) || year%400 == 0)  
  49.             return true;  
  50.         else  
  51.             return false;  
  52.     }  
  53.       
  54.     //计算当月天数  
  55.     static int countMonthDay(int year,int month) {  
  56.         if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||   
  57.                 month == 10 || month == 12) {  
  58.             return 31;  
  59.         }else if(month == 4 || month == 6 || month == 9 || month == 11) {  
  60.             return 30;  
  61.         }else {  
  62.             if(testLeapYear(year))  
  63.                 return 29;  
  64.             else  
  65.                 return 28;  
  66.         }  
  67.     }  
  68.       
  69.     //计算输入年份距离1900年1月1日的总天数  
  70.     static int countDay(int year) {  
  71.         int day = 0;  
  72.         for(int i=1900;i<year;i++) {  
  73.             if(testLeapYear(i))  
  74.                 day += 366;  
  75.             else  
  76.                 day += 365;  
  77.         }  
  78.         return day;  
  79.     }  
  80.       
  81.     //计算输入月份距离输入年份的1月1日的总天数  
  82.     static int countCurrentYear(int year,int month) {  
  83.         int day = 0;  
  84.         for(;month!=1;month--) {  
  85.             day += countMonthDay(year,month);  
  86.         }  
  87.         return day;  
  88.     }  
  89. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖砸5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值