2016-05-15 63 views
-1

對於我的程序,我需要以圖表形式顯示項目描述,數量和價格。這就意味着第1項的描述符合其價格和數量。到目前爲止,我已經嘗試了幾種在Internet上找到的方法,但都沒有成功。我正在使用Dr. Java,所以請建議一些與該編譯器兼容的東西。 提前謝謝!在Java中將字符串格式化爲圖表時最簡單但最有效的方法是什麼?

這是我到目前爲止有:

public static void main(String []args){ 
    Scanner input=new Scanner(System.in); 
    String sentinel = "End"; 

    String description[] = new String[100]; 

    int quantity[] = new int[100]; 

    double price [] = new double[100]; 
    int i = 0; 
    // do loop to get input from user until user enters sentinel to terminate data entry 
    do 
    { 
    System.out.println("Enter the Product Description or " + sentinel + " to stop"); 
    description[i] = input.next(); 

    // If user input is not the sentinal then get the quantity and price and increase the index 
    if (!(description[i].equalsIgnoreCase(sentinel))) { 
     System.out.println("Enter the Quantity"); 
     quantity[i] = input.nextInt(); 
     System.out.println("Enter the Product Price "); 
     price[i] = input.nextDouble(); 
    } 
    i++; 
    } while (!(description[i-1].equalsIgnoreCase(sentinel))); 


    // companyArt(); 
    //System.out.print(invoiceDate()); 
    //System.out.println(randNum()); 



    System.out.println("Item Description: "); 
    System.out.println("-------------------"); 
    for(int a = 0; a <description.length; a++){ 
    if(description[a]!=null){ 
     System.out.println(description[a]); 
    } 
} 
    System.out.println("-------------------\n"); 


    System.out.println("Quantity:"); 
    System.out.println("-------------------"); 
    for(int b = 0; b <quantity.length; b++){ 
    if(quantity[b]!=0){ 
     System.out.println(quantity[b]); 
    } 
    } 
    System.out.println("-------------------\n"); 

    System.out.println("Price:"); 
    System.out.println("-------------------"); 
    for(int c = 0; c <price.length; c++){ 
    if(price[c]!=0){ 
     System.out.println("$"+price[c]); 
    } 
    } 
    System.out.println("-------------------"); 

    //This is where I multiply the price and quantity together to get the total 
    double total = 0.0; 
    for (int j = 0; j < quantity.length; j++){ 

    total += quantity[j] * price[j]; 
    } 

    if(total != 0){ 
    System.out.println("Total: " + total); 
    }  
    } 
} 
+1

使用格式化程序:) –

+1

*「我正在使用Dr. Java,所以請提供與該編譯程序兼容的東西。」* - 編譯器無關緊要。 –

+2

你能舉一個你想要的輸出的例子嗎?我們可以嘗試猜測,但我們爲什麼要這樣做。你需要我們的幫助,所以請具體說明你需要什麼。以及您遇到麻煩的部分。 – Andreas

回答

0

您的代碼打印出來的項目描述的,其次是量的首位,其次是價格列表。我假設這不是你想要的樣子。最好的解決方案是使用一個for循環,每行打印這三項內容。下面的代碼打印出的表格標有「項目說明」,「數量」,和「價格」三列,與表示一個項目的每一行,並且虛線分隔每個行線:

System.out.println("Item Description:\tQuantity:\tPrice:\t"); 
System.out.println("---------------------------------------------------------"); 
for(int a = 0; a <description.length; a++){ 
    if (description[a].equalsIgnoreCase("end")) { 
     break; 
    } 
    System.out.println(description[a] + "\t\t" + quantity[a] + "\t\t" + price[a]); 
    System.out.println("---------------------------------------------------------\n"); 
} 

的輸出將是格式如下:

Item Description: Quantity: Price: 
--------------------------------------------------------- 
asdfaoiew;rjlkf  248   4309.0 
--------------------------------------------------------- 

asodifaasd   43   2323.0 
--------------------------------------------------------- 

asdfasoif   234   2.0 
--------------------------------------------------------- 

如果列不正確對齊,你需要添加或說明後,刪除一個「\ t」的具體取決於您的物品描述多長或短的。

相關問題