下面是使用一维数组来编写2023年的日历的Java代码示例:

public class Calendar2023 {
    public static void main(String[] args) {
        String[] months = {"January", "February", "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"};

        int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int startingDayOfWeek = 2; // January 1, 2023 is a Sunday (0: Sunday, 1: Monday, 2: Tuesday, ...)

        System.out.println("2023");

        for (int month = 0; month < months.length; month++) {
            System.out.println("\n" + months[month]);
            System.out.println("Sun Mon Tue Wed Thu Fri Sat");

            // Print leading spaces
            for (int i = 0; i < startingDayOfWeek; i++) {
                System.out.print("    ");
            }

            // Print days of the month
            for (int day = 1; day <= daysInMonth[month]; day++) {
                System.out.printf("%3d ", day);

                // Move to the next line when it reaches Saturday
                if ((day + startingDayOfWeek) % 7 == 0) {
                    System.out.println();
                }
            }

            // Calculate starting day of the next month
            startingDayOfWeek = (startingDayOfWeek + daysInMonth[month]) % 7;
            
            System.out.println();
        }
    }
}

这个程序使用一个一维数组来存储每个月的名称和每个月的天数。它还使用startingDayOfWeek变量来跟踪每个月的第一天是星期几。通过循环遍历每个月并打印出日历中的日期,然后更新startingDayOfWeek变量以指示下个月的开始日

java写2023年的日历用一维数组

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

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