Java 如何使用printf以表格形式打印

Java 如何使用printf以表格形式打印,java,printf,Java,Printf,我想印一些像这样的东西: =================================================================================================== Section 1 Section 2 Section 3 ----------------------------------------------------------------------------

我想印一些像这样的东西:

===================================================================================================

       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(“\n在可用的门类中为学生提供的第一次分发”);
System.out.println(“==============================================================================================================================================================================================================================================\n”
+“\n”
+“第1节第2节第3节\n”
+ "--------------------------------------------------------------------------------------------------");
对于(int i=0;i<6;i++){
System.out.printf(“%s%s%s%s”,学生[i]。getStudID()+“-”,学生[i]。getFname()+”,学生[i]。getLname()+“-”,学生[i]。getStatus()+”;
System.out.printf(“%s%s%s%s”,学生[i+6]。getStudID()+“-”,学生[i+6]。getFname()+”,学生[i+6]。getLname()+“-”,学生[i+6]。getStatus()+”);
System.out.printf(“%s%s%s%s”,学生[i+12]。getStudID()+“-”,学生[i+12]。getFname()+”,学生[i+12]。getLname()+“-”,学生[i+12]。getStatus()+”);
System.out.println(“”);
}

请为各个部分使用足够的空间,而不是
%s%s%s
,例如,在下面的语句中,
%6s%20s%20s%20s
学生[i]保留6个字符宽的空间。getStudID()
学生[i]保留20个字符宽的空间。getFname()
,为
学生[i]保留20个字符宽的空间。getLname()
,并为
学生[i].getStatus()提供10个字符的宽空间

您可以根据需要调整所需的空间


从的文档中了解更多信息。您还可以查看更多的示例和说明。

了解printf格式的工作原理。“%20s”将始终打印20个字符。从那开始。如果您可以硬编码列宽,您可能只需要知道这些。并且
“%-20s”
在左侧对齐,而
“%20s”
在右侧对齐。这是否回答了您的问题?
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("");
}
System.out.printf("%6s%20s%20s%10s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");