以下是使用一维数组来打印2023年日历,并根据输入月份打印相应月份的代码示例:

import java.util.Scanner;

public class Calendar2023 {
    public static void main(String[] args) {
        // 创建一个二维数组来表示每个月的日历
        int[][] calendar = new int[12][42]; // 12个月,每个月最多占用6行,每行7天

        // 初始化每个月的日历
        initializeCalendar(calendar);

        // 获取用户输入的月份
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入月份(1-12):");
        int month = scanner.nextInt();

        // 打印指定月份的日历
        printCalendar(calendar[month - 1], month);
    }

    // 初始化日历数组
    public static void initializeCalendar(int[][] calendar) {
        for (int i = 0; i < calendar.length; i++) {
            int month = i + 1;
            int daysInMonth = getDaysInMonth(month, 2023);
            int startDayOfWeek = getStartDayOfWeek(month, 2023);

            int day = 1;
            for (int j = startDayOfWeek; j < startDayOfWeek + daysInMonth; j++) {
                calendar[i][j] = day;
                day++;
            }
        }
    }

    // 获取指定月份的天数
    public static int getDaysInMonth(int month, int year) {
        if (month == 2) {
            if (isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        } else {
            return 31;
        }
    }

    // 判断指定年份是否是闰年
    public static boolean isLeapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }

    // 获取指定月份的第一天是星期几
    public static int getStartDayOfWeek(int month, int year) {
        int q = 1; // 每月第一天
        int m = month;
        int K = year % 100; // 年份的后两位数
        int J = year / 100; // 年份的前两位数

        int h = (q + 26 * (m + 1) / 10 + K + K / 4 + J / 4 + 5 * J) % 7;

        return (h + 5) % 7; // 将0表示星期日,1表示星期一,...,6表示星期六
    }

    // 打印指定月份的日历
    public static void printCalendar(int[] monthCalendar, int month) {
        String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
        System.out.println("\n-------------------------");
        System.out.println("      " + month + "月");
        System.out.println("-------------------------");
        for (String weekDay : weekDays) {
            System.out.print(weekDay + "\t");
        }
        System.out.println();

        int rowCount = 0;
        for (int day : monthCalendar) {
            if (day != 0) {
                System.out.print(day + "\t");
            } else {
                System.out.print("\t");
            }

            rowCount++;
            if (rowCount == 7) {
                System.out.println();
                rowCount = 0;
            }
        }
        System.out.println("\n-------------------------");
    }
}

运行示例:

请输入月份(1-12):3

-------------------------
      3月
-------------------------
日	一	二	三	四	五	六	
				1	2	
3	4	5	6	7	8	9	
10	11	12	13	14	15	16	
17	18	19	20	21	22	23	
24	25	26	27	28	29	30	
31	
-------------------------
``
java写2023年的日历用一维数组注释用中文要求我输入的是几月就打印几月的日历

原文地址: https://www.cveoy.top/t/topic/ihQx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录