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

huangapple go评论68阅读模式
英文:

how to use printf to print in a table form

问题

我想要打印出类似这样的内容:

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

       第一部分              第二部分              第三部分
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - 活跃         1116 - Maher Alsolami - 活跃    1122 - Waleed Alamri - 活跃
1111 - Mohammed Turkey - 活跃    1117 - Mohanned Sami - 活跃     1123 - Sami Alghamdi - 活跃
1112 - Sami Omer - 活跃          1118 - Feras Ahmed - 活跃       1124 - Majed Alharbi - 活跃
1113 - Bander Ali - 活跃         1119 - Nawaf Algarni - 活跃     1125 - Anwar Aljohani - 活跃
1114 - Basem Alzahrani - 活跃    1120 - Fahad Alharthi - 活跃    1120 - Fahad Alharthi - 活跃 
1115 - Turkey Almutairi - 活跃   1121 - Anas Batarfi - 活跃
==================================================================================================

但我得到的是:

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

       第一部分              第二部分              第三部分
--------------------------------------------------------------------------------------------------
1110 - Ahmed Sami - 活跃 1116 - Maher Alsolami - 活跃 
1111 - Mohammed Turkey - 活跃 1117 - Mohanned Sami - 活跃 
1112 - Sami Omer - 活跃 1118 - Feras Ahmed - 活跃 
1113 - Bander Ali - 活跃 1119 - Nawaf Algarni - 活跃 
1114 - Basem Alzahrani - 活跃 1120 - Fahad Alharthi - 活跃 
1115 - Turkey Almutairi - 活跃 1121 - Anas Batarfi - 活跃 
==================================================================================================

这是我使用的代码(代码最后会报错,我只想知道如何使其像第一个表格那样排序):

System.out.println("学生在可用部分之间的分配如下:");
System.out.println("===================================================================================================\n"
    + "\n"
    + "       第一部分              第二部分              第三部分\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("");
}
英文:

i want to print some thing like this:

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

       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
==================================================================================================

but i get this :

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

       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 
==================================================================================================

here is the code i use (the code does give an error in the end i just want to know how can sorted like the first table):

System.out.println(&quot;\nThe first distribution for students among the available sectios &quot;);
System.out.println(&quot;===================================================================================================\n&quot;
    + &quot;\n&quot;
    + &quot;       Section 1			  Section 2			  Section 3\n&quot;
    + &quot;--------------------------------------------------------------------------------------------------&quot;);
for (int i = 0; i &lt; 6; i++) {
   System.out.printf(&quot;%s%s%s%s&quot;, students[i].getStudID() + &quot; - &quot;, students[i].getFname() + &quot; &quot;, students[i].getLname() + &quot; - &quot;, students[i].getStatus() + &quot; &quot;);
   System.out.printf(&quot;%s%s%s%s&quot;, students[i + 6].getStudID() + &quot; - &quot;, students[i + 6].getFname() + &quot; &quot;, students[i + 6].getLname() + &quot; - &quot;, students[i + 6].getStatus() + &quot; &quot;);
   System.out.printf(&quot;%s%s%s%s&quot;, students[i + 12].getStudID() + &quot; - &quot;, students[i + 12].getFname() + &quot; &quot;, students[i + 12].getLname() + &quot; - &quot;, students[i + 12].getStatus() + &quot; &quot;);
   System.out.println(&quot;&quot;);
}

答案1

得分: 2

代替%s%s%s%s,为各个部分使用足够的空间,例如在下面的语句中,%6s%20s%20s%20sstudents[i].getStudID() 保留了6个字符宽度的空间,为 students[i].getFname() 保留了20个字符宽度的空间,为 students[i].getLname() 保留了20个字符宽度的空间,以及为 students[i].getStatus() 保留了10个字符宽度的空间。

System.out.printf("%6s%20s%20s%10s", students[i].getStudID() + " - ", students[i].getFname() + " ", students[i].getLname() + " - ", students[i].getStatus() + " ");

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

Formatter的文档中了解更多信息。您还可以查看这个链接获取更多示例和解释。

英文:

Instead of %s%s%s%s, use sufficient space for individual parts e.g. in the following statement, %6s%20s%20s%20s reserves a 6 characters-wide space for students[i].getStudID(), 20 characters-wide space for students[i].getFname(), 20 characters-wide space for students[i].getLname(), and 10 characters-wide space for students[i].getStatus().

System.out.printf(&quot;%6s%20s%20s%10s&quot;, students[i].getStudID() + &quot; - &quot;, students[i].getFname() + &quot; &quot;, students[i].getLname() + &quot; - &quot;, students[i].getStatus() + &quot; &quot;);

You can adjust the required space as per your requirement.

Learn more about it from the documentation of Formatter. You can also check this for some more examples and explanation.

huangapple
  • 本文由 发表于 2020年10月5日 23:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定