<tfoot id='CaRyS'></tfoot>
    1. <small id='CaRyS'></small><noframes id='CaRyS'>

          <bdo id='CaRyS'></bdo><ul id='CaRyS'></ul>

        <legend id='CaRyS'><style id='CaRyS'><dir id='CaRyS'><q id='CaRyS'></q></dir></style></legend>
        <i id='CaRyS'><tr id='CaRyS'><dt id='CaRyS'><q id='CaRyS'><span id='CaRyS'><b id='CaRyS'><form id='CaRyS'><ins id='CaRyS'></ins><ul id='CaRyS'></ul><sub id='CaRyS'></sub></form><legend id='CaRyS'></legend><bdo id='CaRyS'><pre id='CaRyS'><center id='CaRyS'></center></pre></bdo></b><th id='CaRyS'></th></span></q></dt></tr></i><div id='CaRyS'><tfoot id='CaRyS'></tfoot><dl id='CaRyS'><fieldset id='CaRyS'></fieldset></dl></div>
      1. 按列打印 Java 数组

        时间:2023-06-28
      2. <i id='R8pcC'><tr id='R8pcC'><dt id='R8pcC'><q id='R8pcC'><span id='R8pcC'><b id='R8pcC'><form id='R8pcC'><ins id='R8pcC'></ins><ul id='R8pcC'></ul><sub id='R8pcC'></sub></form><legend id='R8pcC'></legend><bdo id='R8pcC'><pre id='R8pcC'><center id='R8pcC'></center></pre></bdo></b><th id='R8pcC'></th></span></q></dt></tr></i><div id='R8pcC'><tfoot id='R8pcC'></tfoot><dl id='R8pcC'><fieldset id='R8pcC'></fieldset></dl></div>

            <bdo id='R8pcC'></bdo><ul id='R8pcC'></ul>

            <tfoot id='R8pcC'></tfoot>
            1. <legend id='R8pcC'><style id='R8pcC'><dir id='R8pcC'><q id='R8pcC'></q></dir></style></legend>

              • <small id='R8pcC'></small><noframes id='R8pcC'>

                  <tbody id='R8pcC'></tbody>

                  本文介绍了按列打印 Java 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试在 Java 中格式化两个数组以打印如下内容:

                  I'm trying to format two arrays in Java to print something like this:

                  Inventory Number      Books                          Prices
                  ------------------------------------------------------------------
                  1                     Intro to Java                  $45.99
                  2                     Intro to C++                   $89.34
                  3                     Design Patterns                $100.00
                  4                     Perl                           $25.00
                  

                  我正在使用以下代码:

                  for(int i = 0; i < 4; i++) {
                          System.out.print(i+1);
                          System.out.print("                     " + books[i] + " ");
                          System.out.print("                 " + "$" + booksPrices[i] + " ");
                          System.out.print("
                  ");
                      }
                  

                  但是我得到了这个格式很差的结果:

                  But I am getting this poorly formatted result instead:

                  Inventory Number      Books                          Prices
                  ------------------------------------------------------------------
                  1                     Intro to Java                  $45.99 
                  2                     Intro to C++                  $89.34 
                  3                     Design Patterns                  $100.0 
                  4                     Perl                  $25.0 
                  

                  如何将所有列直接排列在顶部标题下方?

                  How would I go about lining all the columns up directly under the headers at the top?

                  有没有更好的方法来做到这一点?

                  Is there a better way to go about doing this?

                  推荐答案

                  你应该看看格式:

                  System.out.format("%15.2f", booksPrices[i]);   
                  

                  这将提供 15 个插槽,并在需要时用空格填充它.

                  which would give 15 slots, and pad it with spaces if needed.

                  但是,我注意到您没有右对齐您的数字,在这种情况下,您希望在书籍字段中左对齐:

                  However, I noticed that you're not right-justifying your numbers, in which case you want left justification on the books field:

                  System.out.printf("%-30s", books[i]);
                  

                  这是一个工作片段示例:

                  Here's a working snippet example:

                  String books[] = {"This", "That", "The Other Longer One", "Fourth One"};
                  double booksPrices[] = {45.99, 89.34, 12.23, 1000.3};
                  System.out.printf("%-20s%-30s%s%n", "Inventory Number", "Books", "Prices");
                  for (int i=0;i<books.length;i++){
                      System.out.format("%-20d%-30s$%.2f%n", i, books[i], booksPrices[i]);
                  }
                  

                  导致:

                  Inventory Number    Books                         Prices
                  0                   This                          $45.99
                  1                   That                          $89.34
                  2                   The Other Longer One          $12.23
                  3                   Fourth One                    $1000.30
                  

                  这篇关于按列打印 Java 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java 日期格式 下一篇:为什么选择“010"?等于 8?

                  相关文章

                • <tfoot id='P3vFl'></tfoot>

                    <bdo id='P3vFl'></bdo><ul id='P3vFl'></ul>

                    1. <legend id='P3vFl'><style id='P3vFl'><dir id='P3vFl'><q id='P3vFl'></q></dir></style></legend>

                      <small id='P3vFl'></small><noframes id='P3vFl'>

                      <i id='P3vFl'><tr id='P3vFl'><dt id='P3vFl'><q id='P3vFl'><span id='P3vFl'><b id='P3vFl'><form id='P3vFl'><ins id='P3vFl'></ins><ul id='P3vFl'></ul><sub id='P3vFl'></sub></form><legend id='P3vFl'></legend><bdo id='P3vFl'><pre id='P3vFl'><center id='P3vFl'></center></pre></bdo></b><th id='P3vFl'></th></span></q></dt></tr></i><div id='P3vFl'><tfoot id='P3vFl'></tfoot><dl id='P3vFl'><fieldset id='P3vFl'></fieldset></dl></div>