Java实现日历

import java.util.Scanner;

public class Calendar {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入   年份:");
		int year = in.nextInt();
		System.out.println("请输入   月份:");
		int month = in.nextInt();
		int sum = 0;
		for (int i = 1900; i < year; i++) {
			if (i % 4 == 0 && i % 100 != 0 || year % 400 == 0) {
				sum = sum + 366;
			} else {
				sum = sum + 365;
			}
		}
		for (int i = 1; i < month; i++) {
			if (i == 2) {
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					sum = sum + 29;
				} else {
					sum = sum + 28;
				}
			} else {
				if (i == 4 || i == 6 || i == 9 || i == 11) {
					sum = sum + 30;
				} else {
					sum = sum + 31;
				}
			}
		}
		sum = sum + 1;
		int wekday = sum % 7;
		System.out.println("日\t一\t二\t三\t四\t五\t六");
		for (int i = 1; i <= wekday; i++) {
			System.out.print("\t");
		}
		for (int i = 1; i <= 31; i++) {
			if (sum % 7 == 6) {
				System.out.print(i + "\n");
			} else {
				System.out.print(i + "\t");
			}
			sum++;
		}
	}

}



第一列是在网上看到的一则代码,第二则是一个同学写的代码。第二则定义比较乱,在四十行的位置有“<=”,然后会出现有些月份输出结果是对的,但有些结果是

不整齐的,经过慢慢调试,终于把这个“=”去除掉了。

import java.util.Scanner;

public class Calendar {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int k;
		do {
			System.out.println("请输入年份:");
			int w = in.nextInt();
			System.out.println("请输入月份:");
			int e = in.nextInt();
			int a = 0;
			for (int b = 1900; b < w; b++) {
				if (b % 4 == 0 && b % 100 != 0 || b % 400 == 0) {
					a = a + 366;
				} else {
					a = a + 365;
				}
			}
			for (int b = 1; b < e; b++) {
				if (b == 2) {
					if (w % 4 == 0 && w % 100 != 0 || w % 400 == 0) {
						a = a + 29;
					} else {
						a = a + 28;
					}
				} else if (b == 4 || b == 6 || b == 9 || b == 11) {
					a = a + 30;
				} else {
					a = a + 31;
				}
			}
			a = a + 1;
			System.out.println("日\t一\t二\t三\t四\t五\t六");
			for (int b = 1; b <= (a % 7); b++) {
				System.out.print("\t");
			}
			int p = 1;
			for (; p < e; p++) {
				System.out.print("");
			}
			if (p == 2) {
				if (w % 4 == 0 && w % 100 != 0 || w % 400 == 0) {
					for (int c = 1; c <= 29; c++) {
						if ((a % 7) == 6) {
							System.out.print(c + "\n");
						} else {
							System.out.print(c + "\t");
						}
					}
					a++;
				} else {
					for (int c = 1; c <= 28; c++) {
						if ((a % 7) == 6) {
							System.out.print(c + "\n");
						} else {
							System.out.print(c + "\t");
						}
						a++;
					}

				}
			} else if (p == 4 || p == 6 || p == 9 || p == 11) {
				for (int c = 1; c <= 30; c++) {
					if ((a % 7) == 6) {
						System.out.print(c + "\n");
					} else {
						System.out.print(c + "\t");
					}
					a++;
				}

			} else {
				for (int c = 1; c <= 31; c++) {
					if ((a % 7) == 6) {
						System.out.print(c + "\n");
					} else {
						System.out.print(c + "\t");
					}

					a++;
				}
			}
			System.out.println("\n是否继续查找日期?请输入【1或2】\n1.是\t2.否");
			k = in.nextInt();
		} while (k == 1);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值