0

我的代码有什么问题?出于某种原因,我一直在休息一天?例如,今天是 2013 年 26 日,是星期二,但程序告诉我是星期三。我正在使用 Zeller 的一致性算法。

import javax.swing.JOptionPane;
public class zeller {
    public static void main(String[] args) {
        String yearString = JOptionPane.showInputDialog("Enter the year:");

        int year = Integer.parseInt(yearString);

        String monthString = JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
        int month = Integer.parseInt(monthString);

        String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");  
        int day = Integer.parseInt(dayString);

        switch (month) {
            case 13:  monthString = "January";
                break;
            case 14:  monthString = "February";
                break;
            case 3:  monthString = "March";
                break;
            case 4:  monthString = "April";
                break;
            case 5:  monthString = "May";
                break;
            case 6:  monthString = "June";
                break;
            case 7:  monthString = "July";
                break;
            case 8:  monthString = "August";
                break;
            case 9:  monthString = "September";
                break;
            case 10: monthString = "October";
                break;
            case 11: monthString = "November";
                break;
            case 12: monthString = "December";
                break;
            default: monthString = "Invalid month";
                break;
        }

        int j = year / 100;
        int k = year % 100 ;
        double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +(j / 4) + (5 * j)) % 7;
        int h1 = (int)h;

        switch (h1) {
            case 0:  dayString = "Saturday";
                break;
            case 1:  dayString = "Sunday";
                break;
            case 2:  dayString = "Monday";
                break;
            case 3:  dayString = "Tuesday";
                break;
            case 4:  dayString = "Wednesday";
                break;
            case 5:  dayString = "Thursday";
                break;
            case 6:  dayString = "Friday";
                break;

            default: monthString = "Invalid month";
                break;
        }

        System.out.println("Day of the week is: " + dayString);
    }
}
4

3 回答 3

2
  1. 1 月和 2 月算作上一年的第 13 和第 14 个月,因此如果您希望用户为 1 月或 2 月输入 1 或 2 的实际月份,您可以添加如下代码:
if (month == 1 ){
    month = 13;
     year -=1;
 }
else if (month == 2) {
     month = 14;
     year -=1;
}
于 2013-09-29T01:09:53.780 回答
1

如果是一月或二月,则需要修改年份。维基百科上的解释:

可以很容易地看出,在给定的一年中,3 月 1 日(如果那是星期六,则为 3 月 2 日)是一个很好的考试日期;并且,在给定的世纪中,最好的测试年份是 100 的倍数。Zeller 使用十进制算术,发现使用 J 和 K 来表示年份很方便。但是在使用计算机时,处理修改后的 Y 年会更简单,即1月和 2 月期间的Y - 1 :

我修改了您的代码如下,它可以工作:

import javax.swing.JOptionPane;

public class zeller {
    public static void main(final String[] args) {

        String yearString = JOptionPane.showInputDialog("Enter the year:");

        int year = Integer.parseInt(yearString);

        String monthString =
            JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
        int month = Integer.parseInt(monthString);

        String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
        int day = Integer.parseInt(dayString);

        switch (month) {
        case 14:
            year--;
            monthString = "January";
            break;
        case 13:
            year--;
            monthString = "February";
            break;
        case 3:
            monthString = "March";
            break;
        case 4:
            monthString = "April";
            break;
        case 5:
            monthString = "May";
            break;
        case 6:
            monthString = "June";
            break;
        case 7:
            monthString = "July";
            break;
        case 8:
            monthString = "August";
            break;
        case 9:
            monthString = "September";
            break;
        case 10:
            monthString = "October";
            break;
        case 11:
            monthString = "November";
            break;
        case 12:
            monthString = "December";
            break;
        default:
            monthString = "Invalid month";
            break;
        }
        int j = year / 100;
        int k = year % 100;
        double h = (day + ((13 * (month + 1) / 5)) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
        int h1 = (int) h;
        switch (h1) {
        case 0:
            dayString = "Saturday";
            break;
        case 1:
            dayString = "Sunday";
            break;
        case 2:
            dayString = "Monday";
            break;
        case 3:
            dayString = "Tuesday";
            break;
        case 4:
            dayString = "Wednesday";
            break;
        case 5:
            dayString = "Thursday";
            break;
        case 6:
            dayString = "Friday";
            break;

        default:
            monthString = "Invalid month";
            break;
        }

        System.out.println("Day of the week is: " + dayString);

    }
}
于 2013-02-26T19:03:16.620 回答
0

来自维基百科,

在这个算法中,1 月和 2 月被算作上一年的第 13 个月和第 14 个月。例如,如果是 2010 年 2 月 2 日,则算法将日期计算为 2009 年第 14 个月的第二天(DD/MM/YYYY 格式的 02/14/2009)。

因此,您的代码在技术上没有问题;输入 02/26/2013(今天)作为 2013 年第 14 个月的第 26 天实际上是计算 02/26/ 2014的星期几。

于 2013-02-26T19:13:11.253 回答