【问题标题】:Java: Finding the weekday of a calendar date which is inputted by the userJava:查找用户输入的日历日期的工作日
【发布时间】:2016-01-08 17:01:13
【问题描述】:

我是一名编程爱好者,目前正在学习商业信息学。我们已经开始使用 Java 编程两个月了,我发现这个网站在我必须为我的作业编写一些代码的大多数情况下提供了巨大的帮助。

但是,我必须为此任务实现一个类,该类向用户询问日历日期,验证日期,然后计算并打印该日期的工作日。该程序不能接受不存在的日期。

用户以 YYYYMMDD 格式输入日期。

拒绝任何不可能的日期:

– 1582 年 10 月 15 日之前或 2199 年 12 月 31 日之后的日期

– 不可能的月份(12)

– 不可能的一天(例如,任何一天>31,某个月份的一天>30,某个月份的一天>28 非闰年等)。

要求我们使用Gauss推导出的公式:

A = d + [2.6 x m - 0.2] + y + [y/4]+ [c/4] - (2 x c) W = A % 7

在哪里

  • [x] 是高斯算子:[x] 最大整数 x'

  • d代表天

  • y 表示年份的最后两位数

  • c 代表年份的前两位(实际上是除后两位之外的所有数字;c 代表世纪)

  • m 代表月份

  • w 是工作日

我们还被要求实现一些功能来完成这项任务。请参阅下面的代码:

public class Weekdays {

public static void main(String[] args) {


        TextIO.putln("enter date (yyyymmdd)");
        int date = TextIO.getInt();
        int inDay = date % 100, inMonth = date % 10000 / 100, inYear = date / 10000;
        if(validate(inYear, inMonth, inDay)){
        int W = weekday(inDay, inMonth, inYear);
        String weekday = dayName(W);
        TextIO.putf("%02d.%02d.%4d was/is or will be a %s", date % 100, date % 10000 / 100,
        date / 10000, weekday);
        }else {
        TextIO.putf("invalid date (%d)\n", date);
        }
        }

public static boolean validate (int year) {

    if (year < 1582 || year > 2199) {

        return false;
    }
    else {
        return true;
    }
}

public static boolean validate (int year, int month) {


    if ((year < 1582 || year > 2199)) {

        return false;
    }

    else if (month < 1 || month > 12) {
        return false;
    }

    else if (year == 1582 && month < 10) {

        return false;
    }
    else {
        return true;
    }
}

public static boolean isLeap (int inYear) {
    return (((inYear % 4 == 0) && (inYear % 400 == 0 || inYear % 100 != 0) ));
}

public static int nDays (int month, int year) {

    if ((((year % 4 == 0) && (year % 400 == 0 || year % 100 != 0) ))) {

        if (month == 2) {
            return 29;
        }
        else if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
            return 30;
        }
        else {
            return 31;
        }

    }

        else if (year == 1582 && month == 10) {
            return 16;
    }
        else {

            if (month == 2) {
            return 28;
            }
            else if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
            return 30;
            }
            else {
            return 31;
        }

    }

}

public static boolean validate (int year, int month, int day) {

    if ((year == 1582 && month == 10) && day < 16) {
        return false;
    }
    else if ((year == 1582 && month == 10) && day <= 31) {

    }
    return (validate(year,month) && day > 0 && day <= nDays(month,year));





}

public static int weekday (int inDay, int inMonth, int inYear) {

    int  month, year, day,c;
    year = inYear;
    day = inDay;

    if (inMonth < 3) {
        year--;
        inMonth = inMonth +10;
    }
    else {
        inMonth = inMonth -2;
    }

    month = inMonth;


    c = year/100;
    year = year%100;

    int A = (int) (day+((2.6*month)-0.2)+year+(year/4)+(c/4)-(2*c));

    int x = A % 7;

    if (x < 0) {
        x+=7;
    }

    return x;

}

public static String dayName (int W) {

    switch (W) {

    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";

    default: return "invalid date (" +W+ ")";
    }
    }


}

我在测试两个日期时遇到问题,分别是 21010101 和 19190303。我的程序为这两个日期输出错误的工作日。尽管我尝试了很多,但我根本找不到解决方法。我知道我的代码不是最好的,但作为初学者,我正在尽我所能。任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 今天是几号,预计是几号?
  • 对于 19190303 预计星期一,我得到星期二。对于 21010101 预计周六,我得到周日。
  • 为什么,这块意大利面在其他日期给出正确的结果吗?时间是一件很难搞定的事情,而那些你在上面试图做的事情的库有数百行代码,而你有几十行。你为什么不玩一些更简单的东西,比如图形,把时间留给时间库?
  • 我编辑了帖子以包含指向有关 Gauss' algorithm for determining the day of week 的维基百科文章的链接,我假设它解释了您试图在 weekday 方法中实现的内容。
  • 提示约会?简单。确认日期?简单。但是自己找到星期几?您确定作业实际上希望您这样做,而不是使用标准库函数来获取星期几吗?对我来说,这似乎是一项证明你可以做北京烤鸭的任务,这要求你首先找到一把猎枪并追捕鸭子。

标签: java date weekday


【解决方案1】:

您在计算与A 中的月份值对应的术语时似乎存在舍入错误:

int A = (int) (day+((2.6*month)-0.2)+year+(year/4)+(c/4)-(2*c));

在这里,(2.6*month) 的结果将在减去 0.2 之前被强制转换(“floored”)为int,将工作日设置为带有一些月份的值。您应该提取该术语并使用Math.floor()(“高斯算子”;在公式中用方括号表示)来计算结果:

int monthTerm = (int) Math.floor((2.6 * month) - 0.2);
int A = day + monthTerm + year + (year / 4) + (c / 4) - (2 * c);

(year / 4)(c / 4) 的结果将被隐式“下限”/正确截断,因为两个操作数都是 ints。

【讨论】:

  • 谢谢!我从来没有想过!
【解决方案2】:

使用Math.round(float)。喜欢,

int A = Math.round((day + ((2.6f * month) - 0.2f) + year
        + (year / 4) + (c / 4) - (2 * c)));
int x = A % 7;

我得到(请求的)

01.01.2101 was/is or will be a Saturday

03.03.1919 was/is or will be a Monday

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多