编写一个程序,提示用户输入年份和年份的第一天是星期几,然后显示一年中每个月的第一天。例如,如果用户输入2013年,而2输入2013年1月1日星期二,则程序应显示以下输出:
January 1, 2013 is Tuesday …
December 1, 2013 is Sunday

下面是自己写的代码,写完后和网上比对了一下,基本思路差不多,总结都是如何获取每月第一天是周几,然后输出

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入年份(按回车结束):");
        int year = input.nextInt();
        System.out.print("请输入该年份第一天是星期几:");
        int week = input.nextInt();
        // 获取二月到十二月第一天是星期几
        int[] weeks = getWeek(year, week);
        // 输出结果
        printWeek(weeks, year, week);
    }
    // 获取二月到十二月第一天是星期几, 值0代表周日, 值1代表周一....
    public static int[] getWeek(int year, int week){
        // 获取一月到二月, 一月到三月, 一月到四月....的总天数
        int[] arr = getTotalMonth(year);
        // 第一个对7取余是获得该月第一天离今年第一天的周期日期的距离(一个周期七天)
        int[] day = new int[12];
        for(int i = 0; i < arr.length; i++){
            day[i] = arr[i] % 7;
        }
        // 第二个对7取余是获得该月第一个是周几, 值0代表周日, 值1代表周一....
        int[] weeks = new int[12];
        for(int i = 0; i < day.length; i++){
            weeks[i] = (day[i] + week) % 7;
        }
        return weeks;
    }

    // 返回日期的英语单词
    public static String weekName(int week){
        switch (week){
            case 0:return "Sunday";
            case 1:return "Monday";
            case 2:return "Tuesday";
            case 3:return "Wednesday";
            case 4:return "Thursday";
            case 5:return "Friday";
            case 6:return "Saturday";
        }
        return null;
    }


    // 获取一月到二月, 一月到三月, 一月到四月....的总天数
    public static int[] getTotalMonth(int year){
        int[] arr = new int[12];
        if(isLeapYear(year)){
            arr = new int[]{31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
        }else{
            arr = new int[]{31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
        }
        return arr;
    }

    // 判断是否为闰年
    public static boolean isLeapYear(int year){
        if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
            return true;
        }else{
            return false;
        }
    }

    // 输出结果
    public static void printWeek(int[] weeks, int year, int week){
        System.out.printf("January 1, %d is %s\n", year, weekName(week));
        System.out.printf("February 1, %d is %s\n", year, weekName(weeks[0]));
        System.out.printf("March 1, %d is %s\n", year, weekName(weeks[1]));
        System.out.printf("April 1, %d is %s\n", year, weekName(weeks[2]));
        System.out.printf("May 1, %d is %s\n", year, weekName(weeks[3]));
        System.out.printf("June 1, %d is %s\n", year, weekName(weeks[4]));
        System.out.printf("July 1, %d is %s\n", year, weekName(weeks[5]));
        System.out.printf("August 1, %d is %s\n", year, weekName(weeks[6]));
        System.out.printf("September 1, %d is %s\n", year, weekName(weeks[7]));
        System.out.printf("October 1, %d is %s\n", year, weekName(weeks[8]));
        System.out.printf("November 1, %d is %s\n", year, weekName(weeks[9]));
        System.out.printf("December 1, %d is %s\n", year, weekName(weeks[10]));
    }
}

输入1
请输入年份(按回车结束):2013
请输入该年份第一天是星期几:2
January 1, 2013 is Tuesday
February 1, 2013 is Friday
March 1, 2013 is Friday
April 1, 2013 is Monday
May 1, 2013 is Wednesday
June 1, 2013 is Saturday
July 1, 2013 is Monday
August 1, 2013 is Thursday
September 1, 2013 is Sunday
October 1, 2013 is Tuesday
November 1, 2013 is Friday
December 1, 2013 is Sunday

输入2
请输入年份(按回车结束):2020
请输入该年份第一天是星期几:3
January 1, 2020 is Wednesday
February 1, 2020 is Saturday
March 1, 2020 is Sunday
April 1, 2020 is Wednesday
May 1, 2020 is Friday
June 1, 2020 is Monday
July 1, 2020 is Wednesday
August 1, 2020 is Saturday
September 1, 2020 is Tuesday
October 1, 2020 is Thursday
November 1, 2020 is Sunday
December 1, 2020 is Tuesday

原文地址:http://www.cnblogs.com/Reina-love/p/16817483.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性