java - 如何使用printf以表格形式打印
问题描述
我想打印这样的东西:
===================================================================================================
Section 1 Section 2 Section 3
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - Active 1116 - Maher Alsolami - Active 1122 - Waleed Alamri - Active
1111 - Mohammed Turkey - Active 1117 - Mohanned Sami - Active 1123 - Sami Alghamdi - Active
1112 - Sami Omer - Active 1118 - Feras Ahmed - Active 1124 - Majed Alharbi - Active
1113 - Bander Ali - Active 1119 - Nawaf Algarni - Active 1125 - Anwar Aljohani - Active
1114 - Basem Alzahrani - Active 1120 - Fahad Alharthi - Active 1120 - Fahad Alharthi - Active
1115 - Turkey Almutairi - Active 1121 - Anas Batarfi - Active
==================================================================================================
但我明白了:
===================================================================================================
Section 1 Section 2 Section 3
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - Active 1116 - Maher Alsolami - Active
1111 - Mohammed Turkey - Active 1117 - Mohanned Sami - Active
1112 - Sami Omer - Active 1118 - Feras Ahmed - Active
1113 - Bander Ali - Active 1119 - Nawaf Algarni - Active
1114 - Basem Alzahrani - Active 1120 - Fahad Alharthi - Active
1115 - Turkey Almutairi - Active 1121 - Anas Batarfi - Active
==================================================================================================
这是我使用的代码(代码确实给出了错误,我只想知道如何像第一个表一样排序):
System.out.println("\nThe first distribution for students among the available sectios ");
System.out.println("===================================================================================================\n"
+ "\n"
+ " Section 1 Section 2 Section 3\n"
+ "--------------------------------------------------------------------------------------------------");
for (int i = 0; i < 6; i++) {
System.out.printf("%s%s%s%s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");
System.out.printf("%s%s%s%s", students[i + 6].getStudID() + " - ", students[i + 6].getFname() + " ", students[i + 6].getLname() + " - ", students[i + 6].getStatus() + " ");
System.out.printf("%s%s%s%s", students[i + 12].getStudID() + " - ", students[i + 12].getFname() + " ", students[i + 12].getLname() + " - ", students[i + 12].getStatus() + " ");
System.out.println("");
}
解决方案
代替%s%s%s%s
,为各个部分使用足够的空间,例如在以下语句中,为%6s%20s%20s%20s
保留 6 个字符宽的空间,为 保留students[i].getStudID()
20 个字符宽的空间,为 保留 20 个字符宽的空间students[i].getFname()
,为 保留students[i].getLname()
10 个字符宽的空间students[i].getStatus()
。
System.out.printf("%6s%20s%20s%10s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");
您可以根据需要调整所需的空间。
从Formatter的文档中了解更多信息。您还可以查看此内容以获取更多示例和说明。
推荐阅读
- python - 为什么 Python 中的 `str(1) is '1'` `True` 不是?
- c++ - 可以在 getline 的循环中使用 to_string 吗?
- python - 如何在 SHELL SCRIPT 中比较文件名
- python - 使用 Pycaret 和 plotly 的奇怪时间序列图
- javascript - Angular 10 中的范围滑块不适用于十进制值
- datagrid - 设置文本列过滤器以进行完全匹配检查,而不是“包含”
- android - recyclerview 中的媒体播放器同时播放
- unit-testing - 在 Go 中记录单元测试
- python - 你能改进代码 - 3D 数组中的迭代吗?
- powerbi - SSAS 表格/PowerBI 混合分区