收藏 分销(赏)

输出任意一年的日历.doc

上传人:仙人****88 文档编号:11257592 上传时间:2025-07-11 格式:DOC 页数:3 大小:37KB 下载积分:10 金币
下载 相关 举报
输出任意一年的日历.doc_第1页
第1页 / 共3页
输出任意一年的日历.doc_第2页
第2页 / 共3页


点击查看更多>>
资源描述
输出任意一年的日历 import java.util.Scanner; public class SwitchCycle_03 { static int year,weekDay; //定义静态变量,以便其他类调用 public static boolean isLeapYear(int year) //判断是否为闰年 { return ((year%4==0&&year%100!=0)||year%400==0); } //计算每年的第一天是星期几 public static int firstWeekDayOfYear(int year) { long day=365*(year-1); //从(公元1年的第一天是星期一)开始至今的大致天数 for(int i=1;i<year;i++) //year年的第一天,所以只计算前year年的总天数 { if(isLeapYear(i)) day++; //计算总天数 } return ((int)day%7)+1; } //计算每个月的天数 public static int getMonthOfDays(int month) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if(isLeapYear(year)) return 29; else return 28; default: return 0; } } //日历输出 public static void showMonths() { for(int m=1;m<=12;m++) //逐一打印出月份 { System.out.println(m+"月"); System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday "); for(int j=1;j<=weekDay;j++) { System.out.print(" "); //10格作为开头的间隔 } int monthDay=getMonthOfDays(m); //获取每个月的天数 for(int d=1;d<=monthDay;d++) { if(d<10) System.out.print(" 0"+d+" "); else System.out.print(" "+d+" "); weekDay=(weekDay+1)%7; //判断当天第二天是星期几(输出一天,星期加1) if(weekDay==0) //星期天 System.out.println(); //每个星期换行 } System.out.println(); //每个月换行 print(); } print(); } public static void print() { System.out.println("********************************************************************"); } public static void main(String[] args) { System.out.println("请输入一个年份(公元1年及以后):"); loop:while(true) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); year=Integer.parseInt(str); if(year>=1) { break loop; } System.out.println("请输入正确的年份:"); } weekDay=firstWeekDayOfYear(year); print(); System.out.println("\t\t\t 公元 "+year+" 年 "); print(); showMonths(); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服