首页 > 解决方案 > java中的日历格式

问题描述

我正在尝试制作日历,这部分代码应该打印日期。
现在,当日历完成时,行“|” 停在最后一天。
31 旁边的框应该有一条线。
这就是我到目前为止所拥有的。

    System.out.println("+-----------------------------------------+");
    System.out.println("|               " + months[month] + " " + year + "             |");
    System.out.println("+-----------------------------------------+");
    System.out.println("| Sun | Mon | Tue | Wed | Thu | Fri | Sat |");
    System.out.println("+-----------------------------------------+");

    int d = day(month, 1, year);

    // printing the days
    for (int i = 0; i < d; i++)
    System.out.print("|     ");
    for (int i = 1; i <= days[month]; i++) {
       System.out.printf("|   %2d", i);

        if (((i + d) % 7 == 0) || (i == days[month])) System.out.println("|" + "\n" + "|     |     |     |     |     |     |     |" + "\n" + "+-----+-----+-----+-----+-----+-----+-----+");

    }

}

}

Expected Output (Bottom part)

+-----+-----+-----+-----+-----+-----+-----+
|  26 |  27 |  28 |  29 |  30 |  31 |     |
|     |     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+-----+

Actual Output (Bottom part)


+-----+-----+-----+-----+-----+-----+-----+
|   26|   27|   28|   29|   30|   31|
|     |     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+-----+

标签: javacalendarmonthcalendar

解决方案


没有测试,但System.out.println()改为System.out.print()应该做的伎俩。像这样:

for (int i = 1; i <= days[month]; i++) {
   System.out.printf("|   %2d", i);

   if (((i + d) % 7 == 0) || (i == days[month])) System.out.print("|" + "\n" + "|     |     |     |     |     |     |     |" + "\n" + "+-----+-----+-----+-----+-----+-----+-----+");
}

推荐阅读