我的目标是制作一个日历应用程序,使其输出将完全显示一年的日历。本质上就是这样-
January 2000 February 2000 March 2000
|S M Tu W Th F S| |S M Tu W Th F S| |S M Tu W Th F S|
1 1 2 3 4 5 1 2 3 4
2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11
9 10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18
16 17 18 19 20 21 22 20 21 22 23 24 25 26 19 20 21 22 23 24 25
23 24 25 26 27 28 29 27 26 27 28 29 30 31
但是当我在下面使用我的代码时,我完全无法相信这样的输出。
January 2000 February 2000 March 2000
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 1 2 3 4 5 1 2 3 4
1 1 2 3 4 5 1 2 3 4
1 1 2 3 4 5 1 2 3 4
1 1 2 3 4 5 1 2 3 4
1 1 2 3 4 5 1 2 3 4
April 2000 May 2000 June 2000
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 1 2 3 4 5 6 1 2 3
1 1 2 3 4 5 6 1 2 3
1 1 2 3 4 5 6 1 2 3
1 1 2 3 4 5 6 1 2 3
1 1 2 3 4 5 6 1 2 3
我似乎无法确定问题出在哪里,并且想知道是否存在有关如何解决并使其正常运行的建议。我觉得我没有正确实现一个循环并淘汰了代码中的所有流程,但现在还不确定。
public class Calendar {
public String calender;
/***************************************************************************
* Given the month, day, and year, return which day
* of the week it falls on according to the Gregorian calendar.
* Returns 0 for Sunday, 1 for Monday, and so forth.
***************************************************************************/
public static int day(int month, int day, int year) {
int y = year - (14 - month) / 12;
int x = y + y / 4 - y / 100 + y / 400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (day + x + (31 * m) / 12) % 7;
return d;
}
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0))
return true;
if (year % 400 == 0)
return true;
return false;
}
public static void main(String[] args) {
// take in command line argument to determine the month and year
int month = 0;
int year = 2000;
// try
// {
// year = Integer.parseInt(args[0]); // year
// } catch(Exception e) {
// System.out.println();
// System.out.println();
// System.out.println("Usage: Calender.java [Year<integer>]");
// System.out.println("-h Open Usage Directions");
// System.out.println();
// System.out.println();
// System.exit(0);
// }
// months[i] = name of month i
String[] months = { "", // left empty so that months[1] = "January"
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December" };
// days[i] = number of days in month i
int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (month = 1; month < 11;) {
// check for leap year
if (month == 2 && isLeapYear(year))
days[month] = 29;
// print calendar header
for (int f = 0; f < 3; f++) {
System.out.print(" " + months[month] + " " + year + " ");
month++;
}
System.out.println();
for (int f = 0; f < 3; f++) {
System.out.print("|S M Tu W Th F S|");
}
System.out.println();
// print the calendar
for (int calendarLine = 1; calendarLine <= 5; calendarLine++) {
month-=3;
for (int f = 0; f < 3; f++) {
int d = day(month, 1, year);
for (int i = 0; i < d; i++)
System.out.print(" ");
for (int l = 1; l <= days[month]; l++) {
System.out.printf("%2d ", l);
if (((l + d) % 7 == 0) || (l == days[month])) {
if(month<12)
month++;
break;
}
}
} System.out.println();
}
}