在 Java 中使用 Zeller's Congruence 确定星期几

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17390785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:51:19  来源:igfitidea点击:

Determining Day of the week using Zeller's Congruence in Java

java

提问by George

I have been staring at my Zeller's Congruence program for an hour, but I don't know where is my logic error. Could someone kindly point out the error? Thanks!

我已经盯着我的 Zeller's Congruence 程序看了一个小时,但我不知道我的逻辑错误在哪里。有人可以指出错误吗?谢谢!

// Implement the Zeller's congruence algorithm.
// To calculate the day of the week

import java.util.Scanner;

public class DayOfTheWeek {
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter a year, month and a day
        System.out.print("Enter year (e.g., 2008): ");
        int year = input.nextInt();

        System.out.print("Enter month: 1-12: ");
        int month = input.nextInt();

        System.out.print("Enter the day of the month: 1-31: ");
        int day = input.nextInt();

        // Check if the month is January or February
        // If the month is January and February, convert to 13, and 14,
        // and year has to -1. (Go to previous year).
        if (month == 1 || month == 2) {
            month += 12;
            year--;
        }

        // Compute the answer
        int k = year % 7; // The year of the century
        int j = (int)(year / 100.0); // the century
        int q = day;
        int m = month;
        int h = (q + (int)((26 * (m + 1)) / 10.0) + k + (int)(k / 4.0) 
                + (int)(j / 4.0) + (5 * j)) % 7;

        String result = "Day of the week is ";

        //Display the name of the day of the week
        if (h == 0) 
            System.out.print(result + "Saturday");
        else if (h == 1)
            System.out.print(result + "Sunday");
        else if (h == 2)
            System.out.print(result + "Monday");
        else if (h == 3)
            System.out.print(result + "Tuesday");
        else if (h == 4)
            System.out.print(result + "Wednesday");
        else if (h == 5)
            System.out.print(result + "Thursday");
        else
            System.out.print(result + "Friday");
    }
}

回答by michaelb958--GoFundMonica

int k = year % 7; // The year of the century

You might want % 100there.

你可能想要% 100那里。

Also, you're mixing two of the formulae. If you're after the best implementation in software (as provided by Wikipedia), try this:

此外,您正在混合两个公式。如果您追求软件中的最佳实现(由 Wikipedia 提供),请尝试以下操作:

// ...
// remove j and k
int y = year;
// ...
// reformatted for readability
int h = (q +
         (int)((26 * (m + 1)) / 10.0) +
         y +
         (int)(y / 4.0) +
//       changes after here
         6 * (int)(y / 100.0) +
         (int)(y / 400.0))
        % 7;

or this:

或这个:

int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0)
//                  ^^
           + (int)(j / 4.0) + (5 * j)) % 7;

You were (likely inadvertently) using the start of the second software formula with the end of the first one, causing the computer no end of confusion.

您(可能是无意中)使用了第二个软件公式的开头和第一个公式的结尾,导致计算机无休止地混乱。

回答by Ira Kirk

int h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0)
//                  ^^
       + (int)(j / 4.0) + (5 * j)) % 7;

does not always give the same results as

并不总是给出相同的结果

int h = (q + (int)((26 * (m + 1)) / 10.0) + k + (int)(k / 4.0)
//                  ^^
       + (int)(j / 4.0) + (5 * j)) % 7;

but I appreciate and used your input, Thank you!

但我很感激并使用了您的意见,谢谢!

I also found this helpful:

我也发现这很有帮助:

//to output original year input (k<10?("0" + k):(k)), likewise month and day for format: mm/dd/yyyy;

//输出原始年份输入(k<10?("0" + k):(k)),同样格式为月和日:mm/dd/yyyy;

String result = "The Day of the week of " + (i<10?("0" + i):(i)) + "/" 
//
+ (q<10?("0" + q):(q)) + "/" + j + (k<10?("0" + k):(k)) + " is ";

回答by miko?ak

int k = year % 7; // The year of the century

int k = year % 7; // The year of the century

Shouldn't this be % 100?

这不应该% 100吗?