打印某月的日历

import java.util.Scanner;
public class Demo{
	public static void main(String[]args) {
		Scanner input = new Scanner(System.in);
		int year = 0;
		int moth = 0;
		while (true) {
			System.out.println("请输入要查询的年份:");
			year = input.nextInt();//输入的年-1
			if (year<1900) {
				System.out.println("输入不合法,请重新输入");
				continue;
			}
			break;
		}
		while (true) {
			System.out.println("请输入要查询的月份:");
			moth = input.nextInt();
			if (moth<1 || moth>12) {
				System.out.println("输入不合法,请重新输入");
				continue;
			}
			break;
		}
		//拿到合法的参数  2020年
		//对于年的判断:是不是闰年   365 366
		int count = 365;
		boolean run = false;
		if ((year%4==0&&year%100!=0)||year%400==0) {
			count = 366;
			run = true;
		}
		//2020
		int num = 0;
		for (int i = 1900;i < year;i++) {
			if ((i%4==0&&i%100!=0)||i%400==0) {
				num += 366;
			} else {
				num+=365;
			}
		}
		//num就是输入的年距离1900年1月1日的天数
		//由于1900年1月1日从周一开始的,所以,以天数除以7剩余的就是我们这一年的开始星期数
		//1900年1月1号周一 是  1901年8月1日是周几  500
		//对于月的判断:1 3 5 7 8 10 12   2   剩下
		int mothcount = 0;
		for (int i = 1;i < moth; i++) {
			switch (i) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					num+=31;
					break;
				case 2:
					if (run) {
						num+=29;
					}else {
						num+=28;
					}
					break;
				default:
					num+=30;
			}
		}
		//周几,每个日期是周几,每个月的一号是周几
		switch (moth) {
			case 2:
				if (run) {
						mothcount=29;
					}else {
						mothcount=28;
					}
					break;
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				mothcount = 31;
				break;
			default:
				mothcount = 30;
		}
		//打印效果
		System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
		int number = num%7;
		for (int i = 0;i < number;i++) {
			System.out.print("\t");
		}
		for (int i = 1; i <= mothcount; i++) {
			System.out.print(i+"\t");
			if ((i+number)%7==0) {
				System.out.println();
			}
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值