Java: 查找用户输入的日历日期的工作日

Java: Finding the weekday of a calendar date which is inputted by the user

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

但是,我必须为此执行一个 class 任务,它要求用户输入日历日期,验证日期,然后计算并打印该日期的工作日。该程序不能接受不存在的日期。

用户以 YYYYMMDD 格式输入日期。

拒绝任何不可能的日期:

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

– 不可能的月份(<1 或 >12)

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

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

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

哪里

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

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。我的程序为这两个日期输出了错误的工作日。尽我所能,我根本找不到解决它的方法。我知道我的代码不是最好的,但我正在尽我所能作为初学者。任何帮助,将不胜感激。谢谢!

您在计算与 A 中的月份值相对应的项时似乎存在舍入误差:

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

此处,(2.6*month) 的结果将被转换 ("floored") 为 int,然后再减去 0.2,将工作日设置为带有一些月份值的一个.您应该提取此术语并使用 Math.floor()("Gauss operator";在您的公式中用方括号表示)来计算结果:

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) 的结果将被隐式 "floored" / 正确截断,因为两个操作数都是 int

使用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