我正在尝试制作日历,这部分代码应该打印日期。
现在,当日历完成时,行“|”停在最后日期。
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|
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
最佳答案
没有测试,但将 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" + "+-----+-----+-----+-----+-----+-----+-----+");
}
关于java - java中的日历格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57838658/