1

私は次のように出力を揃えようとしています:

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() )

これが私が得た結果です。総売上を揃えるにはどうすればよいですか? また、2 番目の出力では名前の前にスペースがあります。どうすればそれを取り除くことができますか?

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 
4

3 に答える 3

2

format syntaxに従って、幅を指定する必要があります。

System.out.printf("%-20s %10.2f", list[i].getFirstName()  + " " + list[i].getLastName(), list[i].getTotalSales() );
于 2013-11-13T23:14:24.547 に答える
2
  1. 最長の名前の長さを調べるために、コレクションを反復処理します。
  2. もう一度繰り返しますが、今回は長さ%XXsであるプレースホルダーを使用します。XX名前は、文字までの末尾のスペースで表示されますXX

また、2 番目の出力では名前の前にスペースがあります。どうすればそれを取り除くことができますか?

formatパラメータに余分なスペースを入れました"...%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. の値をlongestNameLength10 ずつ増やしてマージンを作成します。
  2. 合計売上列を右に揃えるには、次のトリックを使用できます。

    System.out.format("%-10s %10s", person.name, String.format("%.2f", person.totalSales));
    
于 2013-11-13T23:14:41.787 に答える
0

これを試して

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

コンソールのこの\tタブは、このタブをどこに置きますか?

于 2013-11-13T23:15:16.033 に答える