2016-05-15 62 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”的具体取决于您的物品描述多长或短的。

相关问题