为什么我的Zeller同音计划总是在一个星期的同一天放一整个月?

时间:2018-04-11 14:17:18

标签: java

我试图编写一个程序,该程序将使用Zeller的一致性来查找星期几中的日期。然而,它一直给我整个月的同一天,因为在1日到31日将在一个星期三的特定月份。谁能看到我做错了什么?

非常感谢任何帮助。

package whatDay;


import java.util.Scanner;

public class DayOfWeek {
public static String year, month, day, date;
public static  int yearInt = 0, monthInt = 0, dayInt = 0;
public static boolean leap = false;
private static Scanner sc;
private static Scanner sc2;
private static Scanner sc3;


public static String inputMonth() {
    sc = new Scanner(System.in);
    System.out.println("Enter the month: ");
    month = sc.next();
    monthInt = Integer.parseInt(month);
    if (monthInt > 12 || monthInt < 1 ) {
        System.out.println("Month must be between 1 and 12. Please try again:  ");
        inputMonth();
    }
    return month;
}

public static String inputYear() {
    sc2 = new Scanner(System.in);
    System.out.println("Enter the year: ");
    year = sc2.next();
    yearInt = Integer.parseInt(year);
    if (yearInt < 0 || yearInt > 8000) {
        System.out.println("Year must be between 0 and 8000. Please try again: ");
        inputYear();
    }
    if ((yearInt % 4 == 0 && yearInt % 100 != 0) || yearInt % 400 == 0) {
        leap = true;
    }
    return year;
}

public static String inputDay() {
    sc3 = new Scanner(System.in);
    System.out.println("Enter the day: ");
    day = sc3.next();
    dayInt = Integer.parseInt(day);

    if (monthInt == 1 || monthInt == 3 || monthInt == 5 || monthInt == 7 || monthInt == 8 || monthInt == 10 || monthInt == 12) {
        if (dayInt < 1 || dayInt > 31) {
            System.out.println("Day must be between 1 and 31: ");
            inputDay();
        }

    }
    else if (monthInt == 2 && leap == false) {
        if (dayInt < 1 || dayInt > 28) {
            System.out.println("Day must be between 1 and 28: ");
            inputDay();
        }
    }
    else if (monthInt == 2 && leap == true) {
        if (dayInt < 1 || dayInt > 29) {
            System.out.println("Day must be between 1 and 29: ");
            inputDay();
        }
    }
    else {
        if (dayInt < 1 || dayInt > 30) {
            System.out.println("Day must be between 1 and 30: ");
            inputDay();
        }
    }
    return day;


}

public static String getDate() {
    inputYear();
    inputMonth();
    inputDay();
    String[] week = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    int h;
    int q = monthInt;
    int m;
    if (monthInt != 1 || monthInt !=  2) {
        m = monthInt;

    }
    else {
        m = monthInt + 12;
        yearInt -= 1;
    }

    h = (q + Math.floorDiv(13*(m+1), 5) + yearInt + Math.floorDiv(yearInt, 4) - Math.floorDiv(yearInt, 100) + Math.floorDiv(yearInt, 400))%7;
    return week[h];
}


public static void main(String[] args) {
    System.out.println(getDate());

}

}

1 个答案:

答案 0 :(得分:1)

你需要改变什么:

星期六在计算中被视为第1天,因此您必须将您的周刊分配更改为:

String[] week = new String[] {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

正确的公式是:

(q + Math.floorDiv(13*(m+1), 5) + yearInt%100 + Math.floorDiv(yearInt%100, 4) - 2*Math.floorDiv(yearInt, 100) + Math.floorDiv(Math.floorDiv(yearInt, 100), 4))%7

q是月份的日期,而不是一年中的某个月,您将错误的变量分配给q,因此,如您所述,结果仅依赖于您当月的输入,则不考虑该日期。

在前两个案例中,您认为必须使用yearInt%100,因为需要本世纪的年份。

减去的部分必须乘以2

在你添加的最后一部分中,你需要进行2层分割,因为可能会四舍五入而不是只进行一次。

编辑:

还有一件事:

if (monthInt != 1 || monthInt !=  2)

这将永远为真,因为所有数字都不是-1或不是-2。将其更改为:

if (monthInt != 1 && monthInt !=  2)