2013-01-19 26 views
7

में 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.