在 Java 中将字符串格式化为图表的最简单但最有效的方法是什么?

What is the simplest but most effective way in formatting strings into a chart in Java?

对于我的程序,我需要以类似图表的格式显示项目描述、数量和价格。因此,这意味着商品 1 的描述与其价格和数量一致。到目前为止,我尝试了几种在网上找到的方法,但都没有成功。我正在使用 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);
   }       
  }
 }

您的代码打印出项目描述列表,然后是数量列表,然后是价格列表。我假设这不是你想要的样子。最好的解决方案是使用单个 for 循环,每行打印这三件事。以下代码打印出一个 table,其中包含标记为 "Item Description"、"Quantity" 和 "Price" 的三列,每行代表一个项目,每行之间用虚线分隔:

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”,具体取决于您的项目描述的长短。