2017-02-09 45 views
0

打印我已經由具有下面的代碼段的開關情況下的餐廳程序:校正對準而在Java

for(int r=0;r<=17;r++) 
{ 
    while(qty[r]>0) 
    { 
     System.out.println(qty[r]+" X "+O[r]+"    "+l[r]); 
     break; 
    } 
} 
//qty has the quantity(int) ; O has the Item name (String) and l has the cost (int) 

由於在字符串的長度的不同,輸出不uniform-它不是沒有正確對齊。

輸出:

********************************************************* 
********************************************************* 
**************************BILL*************************** 

Thu Feb 09 22:01:36 IST 2017 

Your phone number - 9821809843 
Bill number is 4180536 
You have ordered : 
Item Name with quantity   Price 
1 X Fried Raviolli    400 
1 X Potato Fries with Jalapeno and Cheese Dip    800 

The total bill amount is Rs.1200 

Hope you enjoyed. Visit us again soon! 

任何想法,我怎麼能糾正呢?

+0

@ nhouser9是的,這有點解決我的問題。但由於我是新手,我不確定我將如何編輯我的摘錄。請幫我一個忙,告訴我該怎麼做。提前致謝! – LinuxGeek

+0

好的,我在下面發佈了一個答案。如果有幫助,請記住注意並接受。 – nhouser9

回答

0

像這樣的東西更換您的打印語句:

System.out.printf("%d X %-50s %d", qty[r], O[r], l[r]); 

下面是這種方法的一些文檔:http://web.cerritos.edu/jwilson/SitePages/java_language_resources/Java_printf_method_quick_reference.pdf

在一個側面說明,請描述性的命名變量。如果你習慣於命名一個數組lO,你會發現編寫任何更難的項目幾乎是不可能的。註釋

//qty has the quantity(int) O has the Item name (String) and l has the cost (int) 

不應該是必要的,該陣列應該只是被命名爲類似qtynamecost。更好的做法是製作一個存儲所有這三個字段的對象。

+0

此處使用的%是什麼? – LinuxGeek

+0

這個代碼可編譯? –

+0

@LinuxGeek在我的答案中添加一些文檔以解決您的評論 – nhouser9