Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用printf完美格式化_Java - Fatal编程技术网

在Java中使用printf完美格式化

在Java中使用printf完美格式化,java,Java,我正试图让我的程序打印出一张三个项目的清单,上面有它们的名称、数量和价格。一切都很好,我所需要的是如何形成价格和总数,以便让所有的小数每次都排成一行,无论数字有多大。这是我的密码 import java.util.Scanner; class AssignmentOneTest { public static void main(String[] args) { Scanner kb = new Scanner(System.in); // Sys

我正试图让我的程序打印出一张三个项目的清单,上面有它们的名称、数量和价格。一切都很好,我所需要的是如何形成价格和总数,以便让所有的小数每次都排成一行,无论数字有多大。这是我的密码

    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的价格,那么物品价格和小数点总数并不都排成一行,请帮忙。

你必须自己控制

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

第一种方法是在输出之前检查输出的长度,方法是将需要的内容转换为字符串,然后检查其长度。完成后,您可以在价格之间添加空格,使其对齐。当然,像这样的东西可能能够实现这一点,不管您需要什么:

int length = String.valueOf(1000d).length();
我考虑的第二种方法是在价格之间添加标签,使其自动排列。当然,通过这种方式,在所有输出之间的大部分时间都会有额外的空格,并且必须确保项目名称的长度不足以使您需要两个或更多选项卡

祝你好运!如果你需要更多的澄清,请让我知道

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

// calculate padding based on the length of the output
String format = "%" + padding + "d";
System.out.printf(format, variables);
EDIT2:OOP版本,它并不完美,但做得很好:)

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


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

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()
函数中的代码行数。

是的,我对你的意思有点困惑,你能澄清一下吗?也许提供一些示例代码?我已经更新了我的答案。检查最后一个pastebin链接。希望你能合并这样一个系统。我匆匆忙忙地写了一些,所以有些位是不好的(比如公共方法),但逻辑是好的。如果你知道某些字段不会超过一定数量的字符,这是一个很好的方法。我将很快更新我的答案,找到一种基于输入控制填充的方法(虽然可能不是最好的方法)。如果有人感兴趣:)非常感谢,这非常有帮助!helper方法使我的主方法更加清晰well@Mick:是的。当字段超过一定数量的字符时,最好的选择取决于上下文,但我已经编辑了我的答案以应用最简单的解决方案,即截断字符串。对于数字,我不经常发现这样的情况,即人们不能事先决定最大可信数字是什么,并将其与字段宽度相适应。这是真的:)保持这样的简单肯定是最整洁的方法。如果你想知道我是如何做到的,请看一看我的粘贴箱(也在我的答案中)。注意。@Mick:好的解决方案-您的方法的一个优点是,如果某些列很窄,但有些列很宽,您的技术可以充分利用页面上的可用空间,从而最大限度地减少必要的截断量。