java - 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|
| | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+
解决方案
没有测试,但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" + "+-----+-----+-----+-----+-----+-----+-----+");
}
推荐阅读
- jenkins - 无法让 Sonarqube 在 Jenkins 声明式管道中运行
- c# - 如何从我的文件夹中获取缩略图?
- python - 如何使用 spaCy 创建新实体并仅从关键字列表中学习
- java - Spring MVC 初学者
- typo3 - 将自定义函数调用分配给变量
- php - 在没有初始 Sql 数据库的情况下检索 Joomla sql 数据库
- c# - Specflow 功能文件代码-数据定义 c#
- ios - 即使应用程序终止,也会执行后台进程
- node.js - 连接失败时 node-postgres 重新连接
- java - Google Apis Nearest Roads 来截断道路