Java万年历设计

这篇博客介绍了一个Java程序,用于实现万年历功能,包括查询特定年份第一天的星期、判断闰年和月份天数、打印月份日期以及显示特定节日。程序通过用户交互,利用循环、数组和对象,遵循Java编码规范,使用了switch语句进行功能选择,并包含了功能调试和运行。
摘要由CSDN通过智能技术生成

 

一、实验目的

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");
		
    }
    
}

java万年历课程设计,有源代码 源代码片段:public class wannianli extends JFrame implements ActionListener, MouseListener { private Calendar cld = Calendar.getInstance(); private String [] astr = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"}; private DefaultTableModel dtm = new DefaultTableModel(null,astr); private JTable table = new JTable(dtm); //装日期的表格 private JScrollPane sp = new JScrollPane(table); private JButton bLastYear = new JButton("上一年"); private JButton bNextYear = new JButton("下一年"); private JButton bLastMonth = new JButton("上月"); private JButton bNextMonth = new JButton("下月"); private JTextField jtfYear = new JTextField(5);//jtfYear年份显示和输入文本框,允许编辑单行文本 private JTextField jtfMonth = new JTextField(2);//jtfMonth月份显示文本框 private JPanel p1 = new JPanel(); //装入控制日期按钮的模块 private JPanel p2 = new JPanel(); private JPanel p3 = new JPanel(new BorderLayout()); private JPanel p4 = new JPanel(new GridLayout(2,1)); private JPanel p5 = new JPanel(new BorderLayout()); private JButton bAdd = new JButton("保存日志"); private JButton bDel = new JButton("删除日志"); private JTextArea jta = new JTextArea(); //JTextArea 是一个显示纯文本的多行区域 private JScrollPane jsp = new JScrollPane(jta); //管理视口、可选的垂直和水平滚动条以及可选的行和列标题视口 private JLabel l = new JLabel("小提示:可直接输入年份,提高查询效率!"); private JLabel lt = new JLabel();//系统时间 private JLabel ld = new JLabel();//日期是否被选择 private int lastTime; JMenuBar jmb = new JMenuBar(); JMenu view;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值