我正在尝试用 Java 编写一个程序(这是一项学校作业,它告诉你某个日期是星期几。(日期应该写在 yyyy-mm-dd 的表格上。)我以为我来了使用以下代码的解决方案,但后来我发现了一个错误。
当您运行代码并在对话框中输入 1999-12-31 时,程序会告诉您输入的日期 (1999-12-31) 是星期五。但是当您输入日期 2000-01-01(即 1999-12-31 之后的一天)时,程序会告诉您这一天是星期日!星期六怎么了?当您输入 2000-02-29 和 2000-03-01 时,也会出现类似的问题,它们都给出了周三的答案!
我还没有注意到,仅当您输入 2000-01-01 和 2000-02-29 之间的日期时才会出现此错误。如果有人能帮我找出错误的原因并解决问题,我将不胜感激!
import static javax.swing.JOptionPane.*;
import static java.lang.Math.*;
public class DateCalc {
// Here, between the DateCalc class declaration and the main method, several methods used in the program are
// constructed.
// The method isLeapYear tests whether the entered year is a leap year or not.
private static boolean isALeapYear(int year) {
// If the year is a multiple of 4 and not a multiple of 100, or a multiple of 400, then it is a leap year.
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
else {
return false;
}
}
// A method that tests whether a given string is written as a valid date.
private static boolean isAValidDate(int year, int month, int day) {
int maxValidYear = 9999;
int minValidYear = 1754;
if (year > maxValidYear || year < minValidYear) {
return false;
}
if (month < 1 || month > 12) {
return false;
}
if (day < 1 || day > 31) {
return false;
}
繁花如伊
米脂
相关分类