2013-01-19 93 views
7

我正在嘗試創建一個簡單的程序,以獲取三個項目,它們的數量和價格,並將它們全部添加到一起以創建一個簡單的收據類型格式。我的教授給了我一個特定的收據格式,所有小數點排列並始終如一。它應該看起來像這樣。如何在Java中正確使用printf格式

Your Bill: 


Item       Quantity  Price   Total 
Diet Soda       10  1.25   12.50 
Candy        1   1.00   1.00 
Cheese        2   2.00   4.00 


Subtotal             17.50 
6.25% Sales Tax            1.09 
Total              18.59 

我的教授指定的名稱應該有30個字符,10個數量和價格以及總數。這樣做我必須使用printf方法。我試圖用此代碼格式化到目前爲止。

import java.util.Scanner; 
class AssignmentOneTest { 

    public static void main(String[] args) { 
     Scanner kb = new Scanner(System.in); 

     // System.out.printf("$%4.2f for each %s ", price, item); 
     // System.out.printf("\nThe total is: $%4.2f ", total); 

     // process for item one 
     System.out.println("Please enter in your first item"); 
     String item = kb.nextLine(); 
     System.out.println("Please enter the quantity for this item"); 
     int quantity = Integer.parseInt(kb.nextLine()); 
     System.out.println("Please enter in the price of your item"); 
     double price = Double.parseDouble(kb.nextLine()); 

     // process for item two 
     System.out.println("Please enter in your second item"); 
     String item2 = kb.nextLine(); 
     System.out.println("Please enter the quantity for this item"); 
     int quantity2 = Integer.parseInt(kb.nextLine()); 
     System.out.print("Please enter in the price of your item"); 
     double price2 = Double.parseDouble(kb.nextLine()); 
     double total2 = quantity2 * price2; 
     // System.out.printf("$%4.2f for each %s ", price2, item2); 
     // System.out.printf("\nThe total is: $%4.2f ", total2); 

     // process for item three 
     System.out.println("Please enter in your third item"); 
     String item3 = kb.nextLine(); 
     System.out.println("Please enter the quantity for this item"); 
     int quantity3 = Integer.parseInt(kb.nextLine()); 
     System.out.println("Please enter in the price of your item"); 
     double price3 = Double.parseDouble(kb.nextLine()); 
     double total3 = quantity3 * price3; 
     // System.out.printf("$%4.2f for each %s ", price3, item3); 
     // System.out.printf("\nThe total is: $%4.2f ", total3); 

     double total = quantity * price; 

     double grandTotal = total + total2 + total3; 
     double salesTax = grandTotal * (.0625); 
     double grandTotalTaxed = grandTotal + salesTax; 

     String amount = "Quantity"; 
     String amount1 = "Price"; 
     String amount2 = "Total"; 
     String taxSign = "%"; 

     System.out.printf("\nYour bill: "); 
     System.out.printf("\n\nItem"); 
     System.out.printf("%30s", amount); 
     // System.out.printf("\n%s %25d %16.2f %11.2f", item, quantity, price, 
     // total); 
     // System.out.printf("\n%s %25d %16.2f %11.2f", item2,quantity2, price2, 
     // total2); 
     // System.out.printf("\n%s %25d %16.2f %11.2f", item3,quantity3, price3, 
     // total3); 

     System.out.printf("\n%s", item); 
     System.out.printf("%30d", quantity); 
     System.out.printf("\n%s", item2); 
     System.out.printf("\n%s", item3); 

     System.out.printf("\n\n\nSubtotal %47.2f", grandTotal); 
     System.out.printf("\n6.25 %s sales tax %39.2f", taxSign, salesTax); 
     System.out.printf("\nTotal %50.2f", grandTotalTaxed);  
    } 
} 

如果我輸入一個較長的項目名稱,它會移動數量和價格以及總數的位置。 我的問題是,如何使用printf設置有限寬度的設置起點,請幫助。

+0

我希望這會給你一些想法http://stackoverflow.com/questions/8609249/java-printff-string-only-output-formatting – Milan

回答

11
System.out.printf("%1$-30s %2$10d %3$10.2f %4$10.2f", "Diet Soda", 10, 1.25, 12.50); 

將打印線

Diet Soda        10  1.25  12.50 

傳遞給printf方法的第一個字符串是一系列格式定義符來描述我們要如何打印的剩餘參數。

上述格式說明符的語法爲%[index$][flags][width][.precision]conversion,其中[]表示可選。

%開始格式化表達式。

[index]$指示您要格式化參數的索引。索引從1開始。

-上面使用的唯一標誌將輸出對齊到左側。

[width]指示要打印的最少字符數。

.[precision]在這種情況下,小數點後面要寫入的位數,儘管這會隨着不同的轉換而變化。

[conversion]指示參數應該如何格式化的字符。 d是用於十進制整數,f是浮點數的十進制格式,s不會在我們的情況下更改字符串。

的更多信息,可以發現here.