2013-01-19 14 views
7

Próbuję utworzyć prosty program do pobrania trzech elementów, ich ilości i cen oraz dodać je wszystkie razem, aby utworzyć prosty format typu pokwitowania. Mój profesor dał mi specyficzny format potwierdzenia, w którym wszystkie dziesiętne są wyrównane i są konsekwentnie umieszczane. To powinno wyglądać tak.Jak prawidłowo używać formatowania z printf w Javie

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 

Mój profesor stwierdził, że powinno być 30 znaków w nazwie, 10 w ilości i cenie oraz w sumie. Robiąc to, muszę użyć metody printf. Próbuję sformatować go za pomocą tego kodu do tej pory.

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);  
    } 
} 

Jeśli wprowadzę dłuższą nazwę przedmiotu, przeniesie ona rozmieszczenie ilości i ceny oraz sumę. Moje pytanie brzmi: jak ustawić punkt początkowy z ograniczoną szerokością za pomocą printf, proszę o pomoc.

+0

Mam nadzieję, że to daje pewne wyobrażenie http://stackoverflow.com/questions/8609249/java-printff-string-only-output-formatting – Milan

Odpowiedz

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

wydrukuje linię

Diet Soda        10  1.25  12.50 

Pierwszy ciąg przekazany do metody printf to seria wielkoformatowych specyfikatorami które opisują w jaki sposób chcemy reszta argumentów do wydrukowania.

Powyższe specyfikatory formatu mają składnię %[index$][flags][width][.precision]conversion, gdzie [] oznacza opcjonalnie.

% rozpoczyna wyrażenie formatowania.

[index]$ wskazuje indeks argumentu, który ma zostać sformatowany. Indeksy zaczynają się od 1.

- Jedyną flagą używaną powyżej, wyrównuje wyjście w lewo.

[width] oznacza minimalną liczbę znaków do wydrukowania.

.[precision] W tym przypadku liczba cyfr do zapisu po kropce dziesiętnej, choć zależy to od różnych konwersji.

[conversion] znak (y) wskazujące sposób sformatowania argumentu. d jest dla liczby całkowitej dziesiętnej, f jest formatem dziesiętnym dla liczb zmiennoprzecinkowych, s nie zmienia łańcucha w naszym przypadku.

Więcej informacji można znaleźć here.

Powiązane problemy