Java编写万年历

需求

        输入年和月,打印当月的日历

代码

import	java.util.Scanner;
public class test01 {
	/**	
	万年历
    */
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();
		System.out.println("请输入月份:");
		int month = sc.nextInt();

		int allDayOfYear = 0;
		for (int i = 1900; i < year; i++) {
			if (i % 4 == 0 && i % 100!= 0 || i % 400 == 0) {
				allDayOfYear += 366;
			} else {
				allDayOfYear += 365;
			}
		}

		int allDayOfMonth = 0;
		for (int i = 1; i < month; i++) {
			if (i == 2) {
				if (year % 4 == 0 && year % 100!= 0 || year % 400 == 0) {
					allDayOfMonth += 29;
				} else {
					allDayOfMonth += 28;
				}
			}else if (i == 4 || i == 6 || i == 9 || i == 11) {
				allDayOfMonth += 30;
			}else {
				allDayOfMonth += 31;
			}
		}
		System.out.println("获取1月到输入月的总天数:" + allDayOfMonth);
		int allDay = allDayOfMonth + allDayOfYear+1;
		int week = allDay % 7;
		if (week==0){
			week=7;
		}

		int day = 0;
		switch(month){
			case 1:case 3:case 5:case 7:case 8:case 10:case 12:
				day = 31;
			break;
			case 4:case 6:case 9:case 11:
				day =30;
			break;
			case 2:
				if(year%4==0 && year%100!=0 || year%400==0){
					day = 29;
				}else{
					day = 28;
				}
			break;
		}

		System.out.println(year + "年" + month + "月");
		System.out.println("一\t二\t三\t四\t五\t六\t日");
		int count = 0;
		for(int i = 1;i<week;i++){
			System.out.print("\t");
			count++;
		}
		for(int i = 1;i<=day;i++){
			System.out.print(i + "\t");
			count++;
			
			if(count % 7 == 0){
				System.out.println();
			}
		}


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然可以!以下是用 Java 编写的一个简单的万年历代码: ```java import java.util.Scanner; public class Calendar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入年份:"); int year = scanner.nextInt(); System.out.print("请输入月份:"); int month = scanner.nextInt(); printCalendar(year, month); } public static void printCalendar(int year, int month) { // 计算给定年份月份的第一天是星期几 int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印日历表头 System.out.println(" 日 一 二 三 四 五 六"); // 计算并打印日期 int daysInMonth = getDaysInMonth(year, month); int day = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDayOfWeek) { System.out.print(" "); } else if (day > daysInMonth) { break; } else { System.out.printf("%3d", day); day++; } } System.out.println(); } } public static int getFirstDayOfWeek(int year, int month) { // 基姆拉尔森计算公式:W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 if (month < 3) { month += 12; year--; } int century = year / 100; year = year % 100; int dayOfWeek = (year + year / 4 + century / 4 - 2 * century + 26 * (month + 1) / 10) % 7; if (dayOfWeek < 0) { dayOfWeek += 7; } return dayOfWeek; } public static int getDaysInMonth(int year, int month) { int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeapYear(year)) { return 29; } return daysInMonth[month - 1]; } public static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } } ``` 这个代码可以根据用户输入的年份和月份打印出对应的万年历。主要通过计算给定年份月份的第一天是星期几,然后根据星期几来确定日期的排列。其中,`getFirstDayOfWeek` 方法使用了基姆拉尔森计算公式来计算第一天是星期几,`getDaysInMonth` 方法用于获取指定年份月份的天数,`isLeapYear` 方法判断是否为闰年。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值