java - 第二部分 : speeddating object

标签 java

public class Dating
{
// Note: this class has no instance variables!

/**
* Creates an empty Dating object so that you can call the methods
*/

public Dating()
{
// Empty constructor
}

 /**
 * Computes and returns the next year in which New Year's Day will
 * fall on the same day of the week as in a given year
 * @param theYear the given year
 * @return the next year in which New Year's day is the same day
 * of the week as in parameter theYear
 */
 public int newYears(int theYear)
 {
     // TO DO: write body of this method here
 }

/**
 * Computes and returns the Date on which Election Day will fall 
 * in the USA for a given year.
 *
 * NOTE: By law, Thanksgiving  Day is the first Tuesday after the first
 * Monday in November.
 *
 * @param year the year for which to compute the date of Election Day
 * @return the Date of Election Day for the specified year
 */
  public Date electionTime(int year)
 {
    INSERT CODE HERE
  }  

我觉得我的选举时间部分是正确的,但我对新年从哪里开始感到困惑。有什么建议么?我不确定如何编写一个代码,不仅可以计算日期,还可以计算它再次发生的时间。我也没有得到具体的开始年份。

最佳答案

/*
 * Computes and returns the next year in which New Year's Day will
 * fall on the same day of the week as in a given year.
 */
public int newYears(int year)
{
    // First, find out what day of the week it falls on in year X
    Calendar calendar = new GregorianCalendar(); // create a calendar object
    calendar.set(year, 0, 1); // calendar.set([year], January, 1st)
    int day = calendar.get(Calendar.DAY_OF_WEEK); // store this value for later

    // The code between the curly braces below will be executed 30 times,
    // the first time i = 1, the second i = 2, third i = 3, etc...
    for(int i = 1; true; i++)
    {
        calendar.set(year + i, 0, 1); // set the calendar to the next year
        if(calendar.get(Calendar.DAY_OF_WEEK) == day) // compare to the value we stored earlier, and if it's the same day...
        {
            return year + i; // we have the correct year!
        }
    }
}

编辑

好吧,我有点太过分了,但我必须遵循内心极客的召唤。

我使用了一个 for 循环,循环遍历该函数并运行了一系列连续的年份,减去以找出差异,并得到了此表:

  in  |  out  |  difference
 2004    2009     5
 2005    2011     6
 2006    2012     6
 2007    2018     11
 2008    2013     5
 2009    2015     6
 2010    2016     6
 2011    2022     11
 2012    2017     5
 2013    2019     6
 2014    2020     6
 2015    2026     11

有一个非常清晰的模式,每四年重复一次(我想是因为闰年)。使用它,我们可以编写该函数的一个偷偷摸摸/精简的版本:

public int sneakyNewYears(int year)
{
    int diff = year % 4;

    int add = -1;
    if(diff == 0) add = 5;
    if(diff == 1) add = 6;
    if(diff == 2) add = 6;
    if(diff == 3) add = 11;

    return year + add;
}

这在 98.6% 的年份里都可以正常工作,但是针对工作函数测试这个“偷偷摸摸”的函数表明,由于某些奇怪的原因,有几年这不起作用......这些年:1575 , 1577, 1578, 1579, 1580, 1581, 1582, 1691, 1695, 1696, 1697, 1698, 1699, 1700, 1791, 1795, 1796, 1797, 1798, 1799, 1800, 1891、1895、1896、1897、1898 、1899 年和 1900 年。

无论如何。

关于java - 第二部分 : speeddating object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918917/

相关文章:

java - bindFormRequest() 在 Play 2.0.3 中遇到 cannot find symbol 错误

java - 从一个 SQL 表中获取与另一个表有关系的数据计数并在 Java 中使用它

java - java线程中的while循环不运行

javascript - 是否可以将 Scratch 项目转换为 Android 应用程序?

java - JPA:获取引用另一个实体的异常

java - JSP 错误页面会导致问题吗?

java - 用于 api 响应的 Swagger 文档(类型列表)

java - 无法实现不安全线程

java - Kafka命令行consumer读取,但无法通过Java读取

java - RESTful 服务方法可以向客户端发送对象吗?