万年历(Java)

package 万年历算法基础;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;



public class Calendar {

	public static void main(String[] args) 
	{
		// TODO 自动生成的方法存根
		while(true)
		{
			new Menu();
			int x = new Scanner(System.in).nextInt();
			switch(x)
			{
				case 1:
					Scanner sc = new Scanner(System.in);
					System.out.print("请输入查询的年份:");
					int year1 = sc.nextInt();
					if(year1<0) 
					{
						System.out.print("请输入正确的年份!");
						break;
					}
					System.out.print("请输入查询的月份:");
					int month = sc.nextInt();
					if(month<1||month>12) 
					{
						System.out.print("请输入正确的月份!");
						break;
					}
					new YearCalendar(year1,month);
					break;
					
				case 2:
					System.out.print("请输入查询的年份:");
					int year2 = new Scanner(System.in).nextInt();
					if(year2<0) 
					{
						System.out.print("请输入正确的年份!");
						break;
					}
					new YearCalendar(year2);
					break;
					
				case 3:
					for(int i = 0;i < 50;i++)
						System.out.println();
					break;
					
				case 0:System.exit(0);
			}
		}
	}
	
}

class Basis
{
	//打印Y年X月的日历
	public  Basis(int y,int x)
	{
		int len = new JudgeWeek().week(y,x);
		System.out.println("\t\t\t\t"+x+"月");
		System.out.println("=================================================================");
		System.out.println("\t日\t一\t二\t三\t四\t五\t六");

		for(int i = 0;i < len;i++)
			System.out.print("\t ");
		
		for(int i = 1;i <=  new JudgeDay().Days(y, x);i++)
		{
			System.out.print("\t"+i);
			if((len + i) % 7 == 0)
				System.out.println();
		}
		System.out.println("\n=================================================================");
	}
}

/*
 * 上下左右键---完善ing
class Control implements KeyListener
{
	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode()==KeyEvent.VK_DOWN)
		{
			System.out.println("下键");	
		}
		else if(e.getKeyCode()==KeyEvent.VK_UP)
		{
			System.out.println("上键");	
		}
		else if(e.getKeyCode()==KeyEvent.VK_LEFT)
		{
			System.out.println("左键");	
		}
		else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
		{
			System.out.println("右键");	
		}
	}
	public void keyReleased(KeyEvent arg0){
		// TODO 自动生成的方法存根}
	}
	public void keyTyped(KeyEvent arg0) {
		// TODO 自动生成的方法存根}
	}
}
*/
//打印Y年
class YearCalendar 
{
	public YearCalendar(int y)
	{
		
		System.out.println(y+"年:");
		for(int i = 1;i <= 12 ; i++)
			new Basis(y,i);
		while(true)
		{
			System.out.println("A:上一年\tD:下一年\t1:返回菜单");
			String c = new Scanner(System.in).next();
			if(c.equals("1"))
				break;
			if(c.equals("a")&&--y>0)
			{
				for(int i = 0;i < 50;i++)
					System.out.println();
				System.out.println(y+"年:");
				for(int i = 1;i <= 12 ; i++)
					new Basis(y,i);
			}
			if(c.equals("d"))
			{
				for(int i = 0;i < 50;i++)
					System.out.println();
				System.out.println(++y+"年:");
				for(int i = 1;i <= 12 ; i++)
					new Basis(y,i);
			}
	
			
		}
		
	}
	public YearCalendar(int y,int x)
	{
		System.out.println(y+"年:");
		new Basis(y,x);
		while(true)
		{
			System.out.println("A:上一个月\tD:下一个月\t1:返回菜单");
			String c = new Scanner(System.in).next();
			if(c.equals("1"))
				break;
			if(c.equals("a")&&x>1)
			{
				for(int i = 0;i < 50;i++)
					System.out.println();
				new Basis(y,--x);
			}
			if(c.equals("d")&&x<12)
			{
				for(int i = 0;i < 50;i++)
					System.out.println();
				new Basis(y,++x);
			}
	
			
		}
	}
}
//菜单
class Menu
{
	public Menu()
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
		System.out.println("******************************************");	
		System.out.println("                           万年历                            ");
		System.out.println("\t当前时间:"+date+"    ");
		System.out.println("\t    1.查询指定月份                   ");
		System.out.println("\t    2.查询指定年份                  ");
		System.out.println("\t    3.清屏                      ");
		System.out.println("\t    0.退出系统                  ");
		System.out.println("******************************************");
			
	}
}
//判断Y年X月共计几天
class JudgeDay
{
	int[] bigmonth = {1,3,5,7,8,10,12};
	LeapYear year = new LeapYear();
	public int Days(int y,int x)
	{
		if(x==2 && year.leapyear(y) )
			return 29;
		if(x==2)
			return 28;
		for(int i = 0;i < 7;i++)
			if(x == bigmonth[i])
				return 31;
		return 30;
	}
}
//判断当月第一天星期几
class JudgeWeek
{
	public int sum = 0;
	public int week(int y,int x)
	{
		LeapYear year = new LeapYear();
		for(int i = 1;i <= y; i++)
		{
			if(year.leapyear(i))
				sum += 366;
			else
				sum += 365;
		}
		for(int i = 1;i < x ;i++)
			sum += new JudgeDay().Days(y, i);
		
		return sum%7;
	}
}
//判断闰年
class LeapYear
{
	public boolean leapyear(int x)
	{
		if(x%4==0&&x%100!=0||x%400==0)
			return true;
		else
			return false;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值