用java做一个简单的万年历

一个简单的万年历制作

简单介绍万年历的各功能实现:
1.首先键盘输入查询的年份和月份
        Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份");
		int year = sc.nextInt();
		System.out.println("请输入月份");
		int month = sc.nextInt();
		//判断年月是否输入正确
         while(year<1900)
		{
			 System.out.println("你输入的年份不正确,请重新输入年份");
		     year = sc.nextInt();
		}
		while(month>12||month<=0)
		{
			 System.out.println("你输入的月份不正确,请重新输入月份");
		     month = sc.nextInt();
		}
2.然后对该年份的属性进行判断(平年or闰年):
//判断该年是闰年还是平年
	public static boolean YearType(int year)
	{
		if ((year%4==0 &&year%100!=0)||year%400==0)
		{
			return true;
		}else{
			return false;
		}
	}
3.对输入年份每个月的天数进行判断
//判断该年每个月的天数
    public static int day(int month,int year)
	{
		if(month==4||month==6||month==9||month==11){
			return 30;
		}else if(month==2){
			if(YearType(year)){
						return 29;	
					}else{
						return 28;
					}
		}else{
			return 31;
		}
	}
4.打印日历表
public static void WeekTable(int month,int year,int dayall,int monthday){
		//
		System.out.println("日\t"+"一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t");
		for(int i=0;i<=day(month,year)+(dayall+monthday)%7;i++)
		{
			if(i<=(dayall+monthday)%7){
				System.out.print("\t");
			}else{
				System.out.print((i-(dayall+monthday)%7)+"\t");
			}
			if((i+1)%7==0&&i!=0){
				System.out.println();
			}
		}
	}

5.对以上进行整合,最后附上源代码:
import java.util.Scanner;
class Calendar 
{
	public static void main(String[] args) 
	{	
		System.out.println("欢迎进入万历表查询系统");
		System.out.println("---------------------------------------------------");
		boolean button=true;
		while(button!=false){
			Input();
			System.out.println("输入'true'继续查询,'false'退出系统!");
			Scanner sc = new Scanner(System.in);
		    button=sc.nextBoolean();
			if(!button){
				System.out.println("谢谢你的使用,再见!");
			}
	}
	}
	public static void Input(){
		int dayall=0,monthday=0;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份");
		int year = sc.nextInt();
         while(year<1900)
		{
			 System.out.println("你输入的年份不正确,请重新输入年份");
		     year = sc.nextInt();
		}
        System.out.println("请输入月份");
		int month = sc.nextInt();
		while(month>12||month<=0)
		{
			 System.out.println("你输入的月份不正确,请重新输入月份");
		     month = sc.nextInt();
		}
		for (int i=1900+1;i<=year;i++)
		{
			if(YearType(i)){
				dayall+=366;
				//System.out.println("闰年");
			}else{
				dayall+=365;
				//System.out.println("平年");
			}
			
		}
		for (int i=1;i
      
      




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值