查找1900年和用户输入年份之间的每个第一个星期日 [英] Finding every first Sunday of the month between 1900 and a user-input year

查看:222
本文介绍了查找1900年和用户输入年份之间的每个第一个星期日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是家庭作业。

我正在尝试从1900年1月1日起的一个月的第一个月,发现一个星期日登陆的每一次发生(我们假设是一个周一)和用户输入的一年的12月31日。日历扩展程序是禁止的。

I am trying to find every occurrence of a Sunday landing on the first of a month between Jan 1 1900 (which we are assuming was a Monday) and Dec 31 of a year that the user inputs. The calendar extension is off-limits.

我正在以正确的格式返回日期,但不符合我们的教师提供的示例代码。

在给定的示例中,输入1902应该返回:

1 Apr 1900

1七月1900

1月1901年1月
1月1901年1月1日
1月1日1902年

I am returning dates in the correct format, but they do not match up with the example code our instructor provided.
In the given example, an input of 1902 should return:
1 Apr 1900
1 Jul 1900
1 Sep 1901
1 Dec 1901
1 Jun 1902

对于1902年,我的代码返回:

1 1900年3月
1月1901年1月1日
1 1901年4月1日
1901年5月1日
1月1902年2月
1月1902年6月
1902年7月1日

For 1902, my code returns:
1 Mar 1900
1 Jan 1901
1 Apr 1901
1 May 1901
1 Feb 1902
1 Jun 1902
1 Jul 1902

import java.util.Scanner;


public class Sundays {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.print("Enter the ending year: ");
    int userInputYear = reader.nextInt();

    int[] orderedLengthOfMonthsArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    String[] orderedNamesOfMonthsArray = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    int month = 0;
    int dayOfWeek = 1; // initialized to MONDAY Jan 1, 1900 -- Sunday would be #7
    int dayOfMonth = 1;


    for (int year = 1900; year <= userInputYear; year++) {

        for (month = 0; month < orderedLengthOfMonthsArray.length; month++) {


            for (dayOfMonth = 1; dayOfMonth <= orderedLengthOfMonthsArray[month]; dayOfMonth++) {

                dayOfWeek++;

                if (dayOfMonth == 1 && dayOfWeek == 7) {
                    System.out.println(dayOfMonth + " " + orderedNamesOfMonthsArray[month] + " " + year);
                }

                if (dayOfWeek == 8) {
                    dayOfWeek = 1;
                }                   
            }
        }
    }
}  
}


推荐答案

交换if语句并增加 dayOfWeek

Swap the if statement and the increase of dayOfWeek.

        for (dayOfMonth = 1; dayOfMonth <= orderedLengthOfMonthsArray[month]; dayOfMonth++) {
            if (dayOfMonth == 1 && dayOfWeek == 7) {
                System.out.println(dayOfMonth + " " + orderedNamesOfMonthsArray[month] + " " + year);
            }

            dayOfWeek++;

            if (dayOfWeek == 8) {
                dayOfWeek = 1;
            }                   
        }

当你在dayOfMonth 循环,您已经有一周的正确日期(最初是1900年1月1日星期一),所以如果你第一次增加,那么以后的检查将是错误的。

When you are in the dayOfMonth for loop, you already have the correct day of the week (initially Monday, Jan 1st, 1900), so if you first increase it, then the check afterwards would be incorrect.

这篇关于查找1900年和用户输入年份之间的每个第一个星期日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆