Java 为什么我的Zeller';s Congrence计划在一个月内始终在一周的同一天执行?

Java 为什么我的Zeller';s Congrence计划在一个月内始终在一周的同一天执行?,java,Java,我正试图编写一个程序,使用Zeller的同余关系来找出历史上的日期是星期几。然而,在整整几个月里,它一直给我一周中的同一天,因为从1号到31号,在给定的一个月里,所有的日子都是在星期三。有人能看出我做错了什么吗 非常感谢您的帮助 package whatDay; import java.util.Scanner; public class DayOfWeek { public static String year, month, day, date; public static int

我正试图编写一个程序,使用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());

}
package whatDay;
导入java.util.Scanner;
公共课星期五{
公共静态字符串年、月、日、日期;
公共静态int yearInt=0,monthInt=0,dayInt=0;
公共静态布尔leap=false;
专用静态扫描仪sc;
专用静态扫描仪sc2;
专用静态扫描仪sc3;
公共静态字符串inputMonth(){
sc=新扫描仪(系统英寸);
System.out.println(“输入月份:”);
月份=sc.下一个月();
monthInt=Integer.parseInt(月);
如果(monthInt>12 | | monthInt<1){
System.out.println(“月份必须介于1和12之间。请重试:”;
输入月份();
}
返回月份;
}
公共静态字符串inputYear(){
sc2=新扫描仪(System.in);
System.out.println(“输入年份:”);
年份=sc2.next();
yearInt=Integer.parseInt(年);
如果(yearInt<0 | | yearInt>8000){
System.out.println(“年份必须介于0和8000之间。请重试:”;
输入年份();
}
如果((yearInt%4==0&&yearInt%100!=0)| | yearInt%400==0){
闰=真;
}
回归年;
}
公共静态字符串inputDay(){
sc3=新扫描仪(System.in);
System.out.println(“输入日期:”);
day=sc3.next();
dayInt=Integer.parseInt(天);
如果(monthInt==1 | | monthInt==3 | | monthInt==5 | | monthInt==7 | | monthInt==8 | | monthInt==10 | | monthInt==12){
如果(dayInt<1 | | dayInt>31){
System.out.println(“日期必须介于1和31之间:”);
inputDay();
}
}
else if(monthInt==2&&leap==false){
如果(dayInt<1 | | dayInt>28){
System.out.println(“日期必须介于1和28之间:”);
inputDay();
}
}
else if(monthInt==2&&leap==true){
如果(dayInt<1 | | dayInt>29){
System.out.println(“日期必须介于1和29之间:”);
inputDay();
}
}
否则{
如果(dayInt<1 | | dayInt>30){
System.out.println(“日期必须介于1和30之间:”);
inputDay();
}
}
回归日;
}
公共静态字符串getDate(){
输入年份();
输入月份();
inputDay();
字符串[]周=新字符串[]{“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”、“星期日”};
int-h;
int q=monthInt;
int m;
if(monthInt!=1 | | monthInt!=2){
m=蒙蒂特;
}
否则{
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;
返回周[h];
}
公共静态void main(字符串[]args){
System.out.println(getDate());
}

}您必须更改的内容:

在计算中,星期六被视为第一天,因此您必须将周签更改为:

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

在最后一部分中,您添加了需要进行两次楼层划分,因为可能会向下舍入两次而不是一次

编辑:

还有一件事:

if (monthInt != 1 || monthInt !=  2)
这总是正确的,因为所有的数字不是-1就是不是-2。将其更改为:

if (monthInt != 1 && monthInt !=  2)
你的方程式是错误的: