2013-01-19 62 views
7

Tôi đang cố gắng tạo một chương trình đơn giản để lấy ba mục, số lượng và giá của chúng và thêm tất cả chúng lại với nhau để tạo một định dạng loại biên nhận đơn giản. Giáo sư của tôi đã cho tôi một định dạng cụ thể cho biên lai, nơi tất cả các số thập phân xếp hàng và được đặt liên tục. Nó sẽ giống như thế này.Cách sử dụng định dạng với printf chính xác trong Java

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 

Giáo sư của tôi chỉ định có 30 ký tự cho tên, 10 cho số lượng và giá và tổng số. Làm điều này tôi phải sử dụng phương thức printf. Tôi đang cố gắng định dạng nó với mã này cho đến nay.

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

Nếu tôi nhập tên mục dài hơn, nó sẽ di chuyển vị trí số lượng và giá và tổng số. Câu hỏi của tôi là, làm cách nào để đặt điểm bắt đầu bằng chiều rộng giới hạn bằng printf, vui lòng trợ giúp.

+0

Tôi hy vọng điều này sẽ cung cấp cho bạn một số ý tưởng http://stackoverflow.com/questions/8609249/java-printff-string-only-output-formatting – Milan

Trả lời

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

sẽ in dòng

Diet Soda        10  1.25  12.50 

Chuỗi đầu tiên truyền cho phương thức printf là một loạt các định dạng specifiers mô tả cách chúng tôi muốn phần còn lại của các đối số được in ra.

Định dạng thông số định dạng ở trên có cú pháp %[index$][flags][width][.precision]conversion trong đó [] biểu thị tùy chọn.

% bắt đầu một biểu thức định dạng.

[index]$ cho biết chỉ mục của đối số mà bạn muốn định dạng. Chỉ số bắt đầu tại 1.

- Cờ duy nhất được sử dụng ở trên, căn chỉnh đầu ra ở bên trái.

[width] cho biết số ký tự tối thiểu cần in.

.[precision] Trong trường hợp này, số chữ số được viết sau dấu thập phân, mặc dù điều này thay đổi theo các chuyển đổi khác nhau.

[conversion] ký tự cho biết cách định dạng đối số. d là số nguyên thập phân, f là định dạng thập phân cho các dấu phẩy động, s không thay đổi chuỗi trong trường hợp của chúng tôi.

Thông tin chi tiết có thể được tìm thấy here.

Các vấn đề liên quan