0

Zeller の合同プログラムを 1 時間見つめていますが、論理エラーがどこにあるのかわかりません。誰かがエラーを親切に指摘できますか?ありがとう!

// 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");
    }
}
4

3 に答える 3

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

常に同じ結果が得られるわけではありません

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

しかし、私はあなたの入力に感謝し、使用しました、ありがとう!

これも役に立ちました:

// 元の年入力 (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 ";
于 2015-09-15T03:55:24.397 に答える