我的代码出了什么问题?出于某种原因,我一天休息一天?例如,今天是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);
}
}
答案 0 :(得分:2)
if (month == 1 ){
month = 13;
year -=1;
}
else if (month == 2) {
month = 14;
year -=1;
}
答案 1 :(得分:1)
如果是1月或2月,您需要修改年份。关于维基百科的解释:
人们可以很容易地看到,在给定的一年,3月1日(如果那是一个 星期六,然后3月2日)是一个很好的考试日期;在一个给定的 世纪,最佳考试年份是100的倍数。泽勒 使用十进制算术,发现使用J和K很方便 代表年份。但是当使用计算机时,它更简单 处理修改后的年份Y,即1月份的 Y - 1 2月:
我修改了你的代码,如下所示:
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);
}
}
答案 2 :(得分:0)
来自维基百科,
在此算法中,1月和2月计为上一年的第13和第14个月。例如。如果是2010年2月2日,则算法将日期计为2009年第14个月的第二天(2009年2月14日以DD / MM / YYYY格式)。
因此,您的代码在技术上没有任何问题;在2013年2月26日(今天)作为2013年第14个月的第26天实际计算2014/02年/ 的日期。