2

我正在尝试让我的程序打印出三个项目的清单,其中包含它们的名称数量和价格。一切正常,我所需要的只是如何形成价格和总计,以使所有小数每次都对齐,无论数字有多大。这是我的代码

    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("%28s %11s %11s", "Quantity", "Price", "Total");

    //complete item one format
    System.out.printf("\n%-30s", item);
    System.out.printf("%-10d", (int)quantity);
    System.out.printf("%-10.2f", (float)price);
    System.out.printf("  " + "%-10.2f", (float)total);

    //complete item two format
    System.out.printf("\n%-30s", item2);
    System.out.printf("%-10d", (int)quantity2);
    System.out.printf("%-10.2f", (float)price2);
    System.out.printf("  " + "%-10.2f", (float)total2);

    //complete item three format
    System.out.printf("\n%-30s", item3);
    System.out.printf("%-10d", (int)quantity3);
    System.out.printf("%-10.2f", (float)price3);
    System.out.printf("  " + "%-10.2f", (float)total3);




    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);



}

问题是每次价格相同时,一切都排成一行,但是假设我为两个不同的项目输入了 50.00 的价格和 2.50 的价格,那么项目价格和总小数点并不都排成一行, 请帮忙。

4

2 回答 2

8

我发现如果我在一对匹配的函数中进行输出,一个用于标题,一个用于数据,那么排列标题和列会容易得多,例如:

public static void prLine (String item, int quantity, double price, double total) {
    System.out.printf("\n%-20.20s %10d %10.2f %10.2f", item, quantity, 
            price, total);
}   

public static void prTitles () {
    System.out.printf("\n%-20s %10s %10s %10s", "Item", "Quantity", 
            "Price", "Total");
}

您可以看到以这种方式很容易使字段宽度很好地对应。然后我可以按如下方式使用这些功能:

prTitles ();
prLine (item,quantity,price,total);
prLine (item2,quantity2,price2,total2);
prLine (item3,quantity3,price3,total3);

...我得到了我认为你正在寻找的风格的排列输出:

Your bill:
Item                   Quantity      Price      Total
first                         1       1.50       1.50
second                       10      12.50     125.00 
third                       456     322.00  146832.00

将输出代码放在函数中也大大减少了main()函数中的代码行数。

于 2013-01-20T05:19:32.660 回答
2

你必须自己控制它。

基本上我认为有两种不同的方法来处理这个问题。

第一种方法是在输出之前检查输出的长度,方法是将任何必要的内容转换为字符串,然后检查它的长度。完成此操作后,您可以在价格之间添加空格以使它们对齐。像这样的东西可能能够实现这一点,当然集成但是你需要它:

int length = String.valueOf(1000d).length();

我想到的第二种方法是在价格之间添加标签以使其自动排列。当然,这样一来,您在所有输出之间的大部分时间都会有额外的空格,并且您必须确保项目名称不够长,以至于您需要 2 个或更多选项卡。

祝你好运!如果您需要更多说明,请告诉我。

编辑:为了让它更好一点,您可以合并上面的长度检查并使用 printf 的宽度说明符填充空格。它好一点。

// calculate padding based on the length of the output
String format = "%" + padding + "d";
System.out.printf(format, variables);

EDIT2:这个的OOP版本,它并不完美,但做得很好:)

EDIT3:在代码中添加了一些注释。

http://pastebin.com/CqvAiQSg

于 2013-01-20T04:44:55.903 回答