如何使用printf对齐此输出?

时间:2013-11-13 23:07:50

标签: java printf

我正在尝试将输出对齐:

Full Name                Total Sales 
=========                ===========         
John Smith                    619.50 
Mary Willow                   514.50 
Sallie Smite                  519.50 
Tom Andrews                    55.00 
Norman Bates                  366.00 
Horace Williams               301.00 
Anne Whitney                  426.37 
Wallie Jawie                  647.00 
Marie Bunker                   63.00 
Mopsie Bear                  1582.00 
Stephen Andrews               265.00 
Stacie Andrea                 265.00 
Last Name                     463.00 

我正在使用此代码,但对齐始终关闭

// For loop
    System.out.printf("%s %s %.2f %n " , list[i].getFirstName() , list[i].getLastName(), list[i].getTotalSales() )

这是我得到的结果。如何使总销售额保持一致?另外第二个输出在名字前面有一个空格,我怎么能摆脱它呢?

Full Name                Total Sales         
=========                ===========         
John Smith 619.50 
 Mary Willow 514.50 
 Sallie Smite 519.50 
 Tom Andrews 55.00 
 Norman Bates 366.00 
 Horace Williams 301.00 
 Anne Whitney 426.37 
 Wallie Jawie 647.00 
 Marie Bunker 63.00 
 Mopsie Bear 1582.00 
 Stephen Andrews 265.00 
 Stacie Andrea 265.00 
 Last Name 463.00 

3 个答案:

答案 0 :(得分:2)

根据format syntax,应指定宽度。

System.out.printf("%-20s %10.2f", list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales() );

答案 1 :(得分:2)

  1. 对您的收藏进行迭代,以找出最长名称的长度。
  2. 再次迭代,但这次使用%XXs占位符,其中XX是长度。将显示名称,尾随空格最多为XX个字符。
  3.   

    另外第二个输出在名字前面有一个空格,我怎么能摆脱它?

    您已为格式参数"...%n "添加了一个额外的空格,应将其删除。


    public static class Person {
        public String name;
        public double totalSales;
    
        public Person(String name, double totalSales) {
            this.name = name;
            this.totalSales = totalSales;
        }
    }
    
    public static void main (String[] args) throws java.lang.Exception
    {
        List<Person> people = new ArrayList<>();
        people.add(new Person("John Smith", 619.50));
        people.add(new Person("Sallie Smite", 519.50));
        people.add(new Person("Horace Williams", 301.00));
        people.add(new Person("Stacie Andrea", 63.00));
    
        int longestNameLength = 0;
    
        for (Person person : people) {
            int nameLength = person.name.length();
    
            if (nameLength > longestNameLength) {
                longestNameLength = nameLength;
            }
        }
    
        String nameFormat = "%-" + longestNameLength + "s";
    
        System.out.format(nameFormat + " %s%n", "Name", "Total Sales");
    
        for (Person person : people) {
            System.out.format(nameFormat + " %.2f%n", person.name, person.totalSales);
        }
    }
    

    输出

    Name            Total Sales
    John Smith      619.50
    Sallie Smite    519.50
    Horace Williams 301.00
    Stacie Andrea   63.00
    

    提示:

    1. longestNameLength的值增加~10以创建边距。
    2. 要将总销售列对齐,您可以使用以下技巧:

      System.out.format("%-10s %10s", person.name, String.format("%.2f", person.totalSales));
      

答案 2 :(得分:0)

试试这个

//for loop
System.out.printf("%s %s %.2f %n " , list[i].getFirstName() , list[i].getLastName()+"\t"+ list[i].getTotalSales() );

控制台上的这个\t标签将此标签设置为您想要的数量