Java万年历设计

 

一、实验目的

1、巩固并加强Java基础中循环、数组、对象的使用及逻辑能力的提高;

2、熟悉Java代码的编写规范; 

二、实验内容

1、搭建Java编译环境;

2、编写程序实现计算某一年第一天星期几;

3、编写程序实现计算某一年是否为闰年,某一月有多少天;

4、使用循环正确打印一年中的月份及每月日期;

5、学会使用函数解决问题

    6、调试运行程序。 

import java.util.*;
public class 万年历 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("===============欢迎使用Java万年历===============");
		System.out.println("==================请选择功能====================");
		System.out.println("==================0为查询日历====================");
		System.out.println("==================1为结束操作====================");
		int po=0;
		int nian;
		int yue;
		int tianshu;
		int xingqishu;
		int caozuo=0;
		while(po==0) {
			System.out.println("请选择您需要的功能:");
			caozuo=sc.nextInt();
			if(caozuo!=0 && caozuo!=1) {
				System.out.println("==================选择功能错误==================");
			}else {
			//使用“switch”语句进行判断选择需要的功能
			switch (caozuo) {
				case 0:
					System.out.println("===============Java万年历================");
					System.out.print("请输入年份:");
					nian=sc.nextInt();
					System.out.print("请输入月份:");
					yue=sc.nextInt();
					if(nian<1 || yue<1 || yue>12) {
						System.out.println("===============输入错误信息================");
					}else {
						System.out.println("==============="+nian+"年"+yue+"月"+"================");
						Tian tian = new Tian(nian,yue);
						tianshu = tian.jisuantian();
						XingQi xingqi=new XingQi(tianshu);
						xingqishu=xingqi.getXinQi();
						Print print = new Print(xingqishu,nian,yue);
						print.SuanTian();
						print.Shuchu();
						//通过对月份的判断,输出本月的重要节日
						if(yue==1) {
							System.out.println("本月一号为元旦节");
						}else if(yue==3) {
							System.out.println("本月八号为妇女节");
						}
						else if(yue==5) {
							System.out.println("本月一号为劳动节");
						}
						else if(yue==6) {
							System.out.println("本月一号为儿童节");
						}
						else if(yue==7) {
							System.out.println("本月一号为建党节");
						}
						else if(yue==8) {
							System.out.println("本月一号为建军节");
						}
						else if(yue==9) {
							System.out.println("本月十号为教师节");
						}
						else if(yue==10) {
							System.out.println("本月一号为国庆节");
						}
					}
					break;
				case 1:
					System.out.println("操作结束,欢迎下次使用");
					po=1;
			}
		}
			if(po==1) {
				break;
			}
		}
	}
}
//创建计算天数的类
class Tian{
	public int nian;
	public int yue;
	public int yTian;
	public int tian;
	public int i=0;
	//无参构造方法
	public Tian() {
		
	}
	//两参构造方法(传入要查询的年,月)
	public Tian(int nian,int yue) {
		this.nian=nian;
		this.yue=yue;
	}
	//计算出一年一月一日到查询月的天数
	public int jisuantian() {
		for(i=1;i<this.nian;i++) {
			if(i%4==0 && i%100!=0) {
				this.tian=this.tian+366;
		    }else if(i%400==0) {
		    	this.tian=this.tian+366;
		    }else {
		    	this.tian=this.tian+365;
		    }
		}
		for(i=1;i<yue;i++) {
			if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) {
				this.tian=this.tian+31;
			}else if(i==4 || i==6 || i==9 || i==11) {
				this.tian=this.tian+30;
			}else if(i==2) {
				if(i%4==0 && i%100!=0) {
					this.tian=this.tian+29;
				}else if(i%400==0) {
			    	this.tian=this.tian+29;
			    }else {
			    	this.tian=this.tian+28;
			    }
			}
		}
		this.tian=this.tian+1;
		return this.tian;
	}
}
//创建类计算查询月的第一天为星期几
class XingQi{
	public int xingqi;
	//无参构造方法
	public XingQi() {
		
	}
	//通过有参构造方法将计算出来的天数传入
	//通过对传进的参数求余得出星期数
	public XingQi(int tian) {
		this.xingqi=tian%7;
	}
	//返回星期数
	public int getXinQi() {
		return this.xingqi;
	}
}
//创建类将相关信息打印出来
class Print{
	public int xingqi;
	public int nian;
	public int yue;
	public int tian;
	int i=0;
	int u=0;
	int p=1;
	//无参构造方法
	public Print(){
		
	}
	//三参构造方法,将星期数,年,月传入
    public Print(int xingqi,int nian,int yue){
		this.xingqi=xingqi;
		this.nian=nian;
		this.yue=yue;
	}
    //通过判断语句判断出出对应月份的天数
    public void SuanTian() {
    	if(this.yue==1 || this.yue==3 || this.yue==5 || this.yue==7 || this.yue==8 || this.yue==10 || this.yue==12) {
			this.tian=31;
		}else if(this.yue==4 || this.yue==6 || this.yue==9 || this.yue==11) {
			this.tian=30;
		}else if(this.yue==2) {
			if(this.nian%4==0 && this.nian%100!=0) {
				this.tian=29;
			}else if(this.nian%400==0) {
		    	this.tian=29;
		    }else {
		    	this.tian=28;
		    }
		}
    	
    }
    //输出对应日历格式
    public void Shuchu(){
    	System.out.println("======================================");
		System.out.println("   "+"日"+"   "+"一"+"   "+"二"+"   "+"三"+"   "+"四"+"   "+"五"+"   "+"六");
		for(i=1;i<=this.tian;i++) {
			
			if(i==1) {
			for(u=1;u<=this.xingqi;u++) {
				System.out.print("     ");
				p++;
			}
			}
			if(i<10) {
			System.out.print("    "+i);
			}else {
				System.out.print("   "+i);
			}
			if(p==7) {
				System.out.print("\n");
				p=0;
			}
			p++;
		}
		System.out.println("\n");
		
    }
    
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值