如何解决如何使用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
留有足够的空间用于各个部分,例如在以下语句中,students[i].getStudID()
为students[i].getFname()
保留6个字符宽的空间,students[i].getLname()
为20个字符宽的空间,students[i].getStatus()
为20个字符宽的空间System.out.printf("%6s%20s%20s%10s",students[i].getStudID() + " - ",students[i].getFname() + " ",students[i].getLname() + " - ",students[i].getStatus() + " ");
的字符宽空格。
println
您可以根据需要调整所需的空间。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。