星期几,java 和 Zeller 的一致性!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6840020/
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
Day of the week, java, and Zeller's congruence!
提问by aussie_aj
I'm doing this programming exercise out of a textbook where we are given an algorithm for calculating the day of the week called Zeller's congruence. Well do you think I can get the same output as the sample run in the textbook! They go with year 2002, month 3 and day of month 26. The sample reads back Tuesday. I've made several hours of mods and rewrites and can't get anywhere near Tuesday!
我正在根据教科书进行这个编程练习,其中给出了一种算法,用于计算一周中的某天,称为 Zeller 同余。好吧,您认为我可以获得与教科书中运行的示例相同的输出吗!它们与 2002 年、第 3 个月和第 26 个月的日期一致。样本读回星期二。我已经做了几个小时的修改和重写,但在星期二附近无法到达任何地方!
It's page 133 of Java Comprehensive textbook 8e if anyone has it... I'm a beginner so constructive feedback most welcome!
如果有人拥有它,它是 Java 综合教科书 8e 的第 133 页……我是初学者,因此非常欢迎有建设性的反馈!
Your advice would be appreciated:
您的建议将不胜感激:
import java.util.Scanner;
public class DayOfTheWeek {
public static void main(String[] args) {
// Set up the scanner...
Scanner input = new Scanner(System.in);
// Set up the day's of the week with 0 being Sat as per algorithm.
final String[] DAY_OF_WEEK = {"Saturday", "Sunday", "Monday",
"Tuesday", "Wednesday", "Thursday", "Friday"};
// Get the year
System.out.print("Enter the year (e.g., 2002): ");
int year = input.nextInt();
// Get the month
System.out.print("Enter the month 1-12: ");
int month = input.nextInt();
// Get the day
System.out.print("Enter the day 1-31: ");
int day = input.nextInt();
// According to the algorithm Jan is 13 & Feb 14...
if (month == 1) month = 13;
else if (month == 2) month = 14;
// j Is the century.
int j = year / 100;
// k Is the year of the century.
int k = year % 100 ;
// Calculate
double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;
// Cast answer back to integer to get result from array
int ans = (int)h;
// Print result
System.out.println("Day of the week is: " + DAY_OF_WEEK[ans]);
}
}
回答by Jon Lin
It looks like this line of code is wrong:
看起来这行代码是错误的:
double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;
The formula has the dayadded to the first expression, not the month. So it should look like this:
该公式将日期添加到第一个表达式中,而不是月份。所以它应该是这样的:
double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;
回答by Neil Armstrong
Zeller's Congruence requires INTEGER math in order to function properly. Here is the code in C/C++ (which was tested against the PHP day of week function for EVERY day from year 0 to year 10,000). All variables are of type int:
Zeller's Congruence 需要 INTEGER 数学才能正常运行。这是 C/C++ 中的代码(从第 0 年到第 10,000 年的每一天都针对 PHP 星期功能进行了测试)。所有变量都是 int 类型:
day_of_week = (((day + (((month + 1) * 26) / 10) + year + (year / 4) + (6 * (year / 100)) + (year / 400)) - 1) % 7);
Notice the "- 1" near the end of the function - this causes it to return a value from 0 thru 6 rather than 1 thru 7 to make the value easier to use, for example, as an index into a string array of day names.
请注意函数末尾附近的“- 1” - 这会导致它返回一个从 0 到 6 而不是 1 到 7 的值,以使该值更易于使用,例如,作为日期名称字符串数组的索引.
回答by amal
double h = (month + ((26*(month + 1)) / 10) + k + (k / 4) +
(j / 4) + (5 * j)) % 7;
is WRONG . Note that you dont use days in your current implementation.
是错的 。请注意,您在当前的实现中不使用天数。
回答by twister_void
This may work i don't have any idea about that book u may try this code
这可能有效我不知道那本书你可以试试这个代码
import java.util.*;
public class Zeller {
/**
*
* @param args (Not used)
*/
final static String[] DAYS_OF_WEEK = {
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday"
};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the date in dd/mm/yyyy form: ");
String[] atoms = input.nextLine().split("/");
int q = Integer.parseInt(atoms[0]);
int m = Integer.parseInt(atoms[1]);
int y = Integer.parseInt(atoms[2]);
if (m < 3) {
m += 12;
y -= 1;
}
int k = y % 100;
int j = y / 100;
int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
(5 * j)) % 7;
System.out.println("That date was a " + DAYS_OF_WEEK[day] + ".");
}
}
回答by Sam
//just the modified version of the above code.
import java.util.*;
public class Zeller {
final static String[] DAYS_OF_WEEK = {
"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday"
};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the date in dd/mm/yyyy form: ");
String[] atoms = input.nextLine().split("/");
int q = Integer.parseInt(atoms[0]);
int m = Integer.parseInt(atoms[1]);
int y = Integer.parseInt(atoms[2]);
int dd,mm,yy;
dd = q; mm =m; yy=y;
if (m < 3) {
m += 12;
y -= 1;
}
int k = y % 100;
int j = y / 100;
int day = ((q + (((m + 1) * 26) / 10) + k + (k / 4) + (j / 4)) +
(5 * j)) % 7;
Calendar now = Calendar.getInstance();
int nd,nm,ny;
nm = now.get(Calendar.MONTH) + 1;
nd = (now.get(Calendar.DATE));
ny = now.get(Calendar.YEAR);
if(dd == nd && mm == nm && yy == ny)
present(day);
else if(yy<ny)
past(day);
else if(yy == ny)
if(mm == nm)
if(dd>nd)
future(day);
else
past(day);
else if(mm>nd)
future(day);
else
past(day);
else
future(day);
}
public static void past(int day)
{
System.out.println("That date was " + DAYS_OF_WEEK[day] + ".");
}
public static void future(int day)
{
System.out.println("That date will be " + DAYS_OF_WEEK[day] + ".");
}
public static void present(int day)
{
System.out.println("Toady is " + DAYS_OF_WEEK[day] + ".");
}
}
回答by omkarstha
import java.util.Scanner;
public class zelleralgorithm {
public static void main (String[] args) {
int month, dayOfMonth, year, cenNumber, yearNumber, weekday, counter = 0;
String dayname = null;
Scanner scan = new Scanner(System.in);
System.out.println("\tZeller's Algorithm");
System.out.println("**************************************");
System.out.print("Enter month ( or 0 to exit):\t");
month = scan.nextInt();
//handling exception
while(month > 12){
System.out.println("Please enter a valid month!\n");
System.out.print("Enter month ( or 0 to exit):\t");
month = scan.nextInt();
}
while(month != 0) {
System.out.print("Enter day:\t\t\t");
dayOfMonth = scan.nextInt();
//handling exception
while(dayOfMonth > 32){
System.out.println("Please enter a valid date!\n");
System.out.print("Enter day:\t\t\t");
dayOfMonth = scan.nextInt();
}
System.out.print("Enter year:\t\t\t");
year = scan.nextInt();
if(month == 1 || month == 2){
month = 11;
--year;
}
else{
month = month -2;
}
cenNumber = year / 100;
yearNumber = year % 100;
weekday = (( (int) (2.6*month-.2) + dayOfMonth + yearNumber + (yearNumber/4) + (cenNumber/4) - (2*cenNumber)) % 7);
if(weekday < 0){
weekday = weekday + 7;
}
switch(weekday){
case 0:
dayname = "Sunday";
break;
case 1:
dayname = "Monday";
break;
case 2:
dayname = "Tuesday";
break;
case 3:
dayname = "Wednesday";
break;
case 4:
dayname = "Thursday";
break;
case 5:
dayname = "Friday";
break;
case 6:
dayname = "Saturday";
break;
default:
dayname = "Exceptional Case error!!";
}
System.out.println("\n**************************************");
System.out.println("\tThe day is "+ dayname);
System.out.println("**************************************");
System.out.print("\nEnter month ( or 0 to exit):\t");
month = scan.nextInt();
++counter;
}
//while loop end
//counter
System.out.println("Number of entries = " + counter);
scan.close();
}
}