我的代码编译并完美运行,但是当我周六的任何日期时,输出为'null'而不是"星期六"。下面将提供示例以进一步解释此问题。
我试图更改我在" getDayofweek"方法上的说法,但我似乎没有解决方案,我也试图从经验丰富的编码器那里获得帮助,但是由于Java不是他们的主要语言,他们似乎在挣扎...
代码:
class MyDate {
// properties of date object
private int day, month, year;
// constructor with arguments
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public boolean isDateValid() {
if (month > 12 || month < 1 || day < 1) { // if values exceed 12 or are negative: return false
return false;
} else if (year <= 1582 && month <= 10 && day <= 15) { // starting date
// checking
return false;
} // for 31 day months: January, March, May, July, August, October, December
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) {
return false;
}
} // for 30 day months: April, June, September, November
else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
return false;
}
} // February check
else if (month == 2) {
// leap year check for February
// 29 days in a leap year
// 28 days in a common year
if (isLeapYear()) {
if (day > 29) {
return false;
}
} else {
if (day > 28) {
return false;
}
}
}
return true;
}
// checks if input year is leap year
private boolean isLeapYear() {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
// method returns the day of MyDate object
public int getDay() {
return day;
}
// parameter for day to set
public void setDay(int day) {
this.day = day;
}
// method returns the month of MyDate object
public int getMonth() {
return month;
}
// parameter for month
public void setMonth(int month) {
this.month = month;
}
// method returns the year of MyDate object
public int getYear() {
return year;
}
// parameter for year
public void setYear(int year) {
this.year = year;
}
// method returns all variables: day/month/year of MyDate object
public String toString() {
return day + "/" + month + "/" + year;
}
}
public class MyCalendar {
//enums for days of week
public static enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
};
//enums for month of year
public static enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
};
//enums for week number of month
public static enum Week {
FIRST, SECOND, THIRD, FOURTH, FIFTH;
};
//to store Date object
private MyDate date;
//constructor taking mydate object
public MyCalendar(MyDate enteredDate) {
this.date = enteredDate;
}
//main method
public static void main(String[] args) {
boolean dateValid = false; //valid date false
Scanner input = new Scanner(System.in); //scanner for input
MyDate enteredDate = null;
//till valid date found
while (!dateValid) {
System.out.print("Enter the date as day month year : ");
//taking input and creating date output
enteredDate = new MyDate(input.nextInt(), input.nextInt(), input.nextInt());
//validating date input
if (enteredDate.isDateValid()) { //if valid
MyCalendar myCalendar = new MyCalendar(enteredDate);
//creating calendar table
myCalendar.printDateInfo(); //printing date info
myCalendar.printCalendar(); //printing calendar
dateValid = true; //setting validate to true
} else {
System.out.println(enteredDate + " is not a valid date, please re-input a valid date: ");
}
}
input.close();
}
// returns number of days in current month
private int getNumberOfDays() {
int days = 31;
int month = date.getMonth();
if (month == 4 || month == 6 || month == 9 || month == 11)
days = 30;
return days;
}
//print calendar of input month
public void printCalendar() {
System.out.println("nnThe Calendar of "+Month.values()[date.getMonth()-1]+" "+date.getYear()+" is :");
int numberOfMonthDays = getNumberOfDays();
Day firstWeekdayOfMonth = getDayOfWeek(1, date.getMonth(), date.getYear());
int weekdayIndex = 0;
System.out.println("Su Mo Tu We Th Fr Sa");
// The order of days depends on the input of date
// to display output of calendar
for (int day = 0; Day.values()[day] != firstWeekdayOfMonth; day++) {
System.out.print(" "); // this loop to print the first day in the
// correct place
weekdayIndex++;
}
for (int day = 1; day <= numberOfMonthDays; day++) {
if (day < 10)
System.out.print(day + " ");
else
System.out.print(day);
weekdayIndex++;
if (weekdayIndex == 7) {
weekdayIndex = 0;
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
//method to print about date information in literal form
public void printDateInfo() {
System.out.println(date + " is a " + getDayOfWeek(date.getDay(), date.getMonth(), date.getYear())
+ " located in the " + Week.values()[getWeekOfMonth() - 1] + " week of "
+ Month.values()[date.getMonth() - 1] + " " + date.getYear());
}
/*
* gets day of the week, returns enum type Day
*
* Zellar's congruence to calculate the day of week
* for any given date after October 15 1582
* (h) = (q+(13*(m+1)/5)+K+(K/4)+(J/4)+5J)%7 ,q- day of month,
* m- month, k = year of century (year%100), J = (year/100)
*/
public Day getDayOfWeek(int day, int month, int year) {
int q = day;
int m = month;
if (m < 3) {
m = 12 + date.getMonth();
year = year - 1;
}
int K = year % 100;
int J = year / 100;
//calculating h value
int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
Day output = null;
if (h < Day.values().length && h > 0) {
output = Day.values()[h - 1]; //getting respective enum value
}
return output; //returning enum value
}
// get week number of current date
public int getWeekOfMonth() {
int days = date.getDay();
int weeks = days / 7;
days = days % 7;
if (days > 0)
weeks++;
return weeks;
}
}
预期结果:
java MyCalendar 29/02/2019
29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019
25/05/2019 is a Saturday and located in the fourth week of May 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
实际结果:
25/05/2019 is a null located in the FOURTH week of MAY 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
另一个日期的结果(不是星期六(:
24/05/2019 is a FRIDAY located in the FOURTH week of MAY 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
指定的问题:输出打印在周六当天的所有日期上,而不是星期六的其他日期将达到所需的正确输出。
if (h < Day.values().length && h > 0) {
应该更改为
if (h < (Day.values().length + 1) && h > 0) {
- 如果这是针对生产代码的,那么Basil Bourque的评论是正确的:您不应该开发自己的
MyDate
类,而应依靠LocalDate
中的内置。 - 另一方面,我认为这是一个编程练习,这是一个很好的练习,而且没有理由(我可以看到(为什么您不应该通过它来挣扎。
您计算一周一天的公式是:
//calculating h value
int h = (q + (13 * (m + 1) / 5) + K + (K / 4) + (J / 4) + 5 * J) % 7;
(顺便说一句,请找到更好的变量名称,也尊重Java命名约定:变量名不能是大写字母。(我不明白公式,但是假设它是正确的,它可以计算一天每周0 =星期六,1 =周日最多6 =星期五。要将此数字用作查找您的Day
枚举
output = Day.values()[(h + 6) % 7]; //getting respective enum value
由于h
始终是非负的,小于7,因此您不需要封闭的if
语句。只需无条件分配给output
即可。通过这些更改,我得到
Enter the date as day month year : 25 5 2019 25/5/2019 is a SATURDAY located in the FOURTH week of MAY 2019 The Calendar of MAY 2019 is : Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31