<small id='0XYPl'></small><noframes id='0XYPl'>

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

      按列打印 Java 数组

      时间:2023-09-29

        <tbody id='sQQSs'></tbody>
        <tfoot id='sQQSs'></tfoot>

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

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

              <legend id='sQQSs'><style id='sQQSs'><dir id='sQQSs'><q id='sQQSs'></q></dir></style></legend>

                本文介绍了按列打印 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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                相关文章

                最新文章

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

                  1. <tfoot id='K9bT0'></tfoot>

                  2. <small id='K9bT0'></small><noframes id='K9bT0'>