2016-08-30 6 views
2

(領収書を作成します)私は、次のコードが見つかりました:、この 'String#format'パターンの意味は何ですか?タスクのための研究中に

System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel)); 

私はSystem.out.printlnが何のためにあるのかを理解しない:

public class Kassenzettel { 
    public static void main(String[] args) { 

     // Einzelpreise - Waren 
     double SEBox = 299.00; 
     double Hauptplatine = 79.80; 
     double Erweiterungsplatine =52.60; 
     double Anschlusskabel = 19.50; 
     double Firmware = 36.00; 

     // Anzahl - Waren 
     int anzSEBox = 1; 
     int anzHauptplatine = 1; 
     int anzErweiterungsplatine = 1; 
     int anzAnschlusskabel = 2; 
     int anzFirmware = 2; 

     // Inhalt - Brieftasche 
     double brieftasche = 550.00; 

     // Gekaufte Waren 
     double summe = 0; 
     summe = summe + anzSEBox * SEBox; 
     summe = summe + anzHauptplatine * Hauptplatine; 
     summe = summe + anzErweiterungsplatine * Erweiterungsplatine; 
     summe = summe + anzAnschlusskabel * Anschlusskabel; 
     summe = summe + anzFirmware * Firmware; 

     if (summe > brieftasche) { 
      System.out.println("Nicht genuegend Geld in der Brieftasche"); 
     } else { 

      System.out.println("____________________________________"); 
      System.out.println("DATCOM protelematik GmbH - Kassenbon"); 
      System.out.println("____________________________________"); 

      System.out.println(String.format("%-9s %2d x %5.2f EUR", "SEBox", anzSEBox, SEBox)); 
      System.out.println(String.format("%30.2f EUR", anzSEBox * SEBox)); 

      System.out.println(String.format("%-9s %2d x %5.2f EUR", "Hauptplatine", anzHauptplatine, Hauptplatine)); 
      System.out.println(String.format("%30.2f EUR", anzHauptplatine * Hauptplatine)); 

      System.out.println(String.format("%-9s %2d x %5.2f EUR", "Erweiterungsplatine", anzErweiterungsplatine, Erweiterungsplatine)); 
      System.out.println(String.format("%30.2f EUR", anzErweiterungsplatine * Erweiterungsplatine)); 

      System.out.println(String.format("%-9s %2d x %5.2f EUR", "Anschlusskabel", anzAnschlusskabel, Anschlusskabel)); 
      System.out.println(String.format("%30.2f EUR", anzAnschlusskabel * Anschlusskabel)); 

      System.out.println(String.format("%-9s %2d x %5.2f EUR", "Firmware", anzFirmware, Firmware)); 
      System.out.println(String.format("%30.2f EUR", anzFirmware * Firmware)); 

      System.out.println("____________________________________"); 

      System.out.println(String.format("%-9s %20.2f EUR", "Gesamt", summe)); 
      System.out.println(String.format("%-9s %20.2f EUR", "Gegeben", brieftasche)); 
      System.out.println(); 
      System.out.println(String.format("%-9s %20.2f EUR", "Zurueck", brieftasche - summe)); 
     } 
    } 
} 

をしかし、私は次の行を理解していません誰かが私にこのことがどういうものなのかを説明することができます:"%-9s %2d x %5.2f EUR"

私はそれはコードが言う一部のためだと思う:

SEBox  1 x 299,00 EUR 
         299,00 EUR 
Hauptplatine 1 x 79,80 EUR 
         79,80 EUR 
Erweiterungsplatine 1 x 52,60 EUR 
         52,60 EUR 
Anschlusskabel 2 x 19,50 EUR 
         39,00 EUR 
Firmware 2 x 36,00 EUR 
         72,00 EUR 

が、なぜ、どのように?

+5

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html –

答えて

5

String.formatは、文字列をフォーマットするために使用されます。

%は、フォーマット指示が続くことを示す特殊文字です。

引数の型は、d,f,xおよびsで表されます。

  • 整数 - D、

  • ストリングS - 、

  • 浮動小数点 - ヘキサformat- Xと

  • 整数F。

%-9s:文字列をそのままフォーマットします。文字列が9文字未満の場合、出力は右側に埋め込まれます。

%2d:整数をそのまま書式設定します。桁数が2未満の場合、出力は左にパディングされます。

%5.2f:数字の2桁の小数点以下をフォーマットします。出力は少なくとも5文字を占有します。桁数が足りない場合は、詰め込まれます。

関連する問題