如何在Java中正确使用printf格式化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中正确使用printf格式化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4165字,纯文字阅读大概需要6分钟。
内容图文
![如何在Java中正确使用printf格式化](/upload/InfoBanner/zyjiaocheng/706/b6694137b54a40d6b753fd0045a40258.jpg)
我正在尝试创建一个简单的程序来接收三个项目,它们的数量和价格,并将它们全部加在一起以创建一个简单的收据类型格式.我的教授给了我一个特定的收据格式,其中所有小数都排成一行并且一致地放置.它看起来应该是这样的.
Your Bill:
Item Quantity Price Total
Diet Soda 10 1.25 12.50
Candy 1 1.00 1.00
Cheese 2 2.00 4.00
Subtotal 17.50
6.25% Sales Tax 1.09
Total 18.59
我的教授指出名称应该有30个字符,数量和价格应该是10个字符和总数.这样做我必须使用printf方法.到目前为止,我正在尝试使用此代码对其进行格式化.
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("%30s", amount);
// System.out.printf("\n%s %25d %16.2f %11.2f", item, quantity, price,
// total);
// System.out.printf("\n%s %25d %16.2f %11.2f", item2,quantity2, price2,
// total2);
// System.out.printf("\n%s %25d %16.2f %11.2f", item3,quantity3, price3,
// total3);
System.out.printf("\n%s", item);
System.out.printf("%30d", quantity);
System.out.printf("\n%s", item2);
System.out.printf("\n%s", item3);
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);
}
}
如果我输入更长的商品名称,则会移动数量,价格和总数的位置.
我的问题是,如何使用printf创建一个有限宽度的设置起点,请帮忙.
解决方法:
System.out.printf("%1$-30s %2$10d %3$10.2f %4$10.2f", "Diet Soda", 10, 1.25, 12.50);
将打印线
Diet Soda 10 1.25 12.50
传递给printf方法的第一个字符串是一系列格式说明符,用于描述我们希望如何打印其余参数.在格式说明符周围,您可以添加其他也将打印的字符(不进行格式化).
上面的格式说明符具有以下语法:
%[index $] [flags] [width] [.precision]转换,其中[]表示可选.
%开始格式化表达式.
[index $]表示要格式化的参数的索引.索引从1开始.上面的索引实际上并不需要,因为没有索引的每个格式说明符都被指定为从1开始计数.
[ – ]上面使用的唯一标志,将输出对齐到左侧.
[width]表示要打印的最小字符数.
[.precision]在这种情况下,小数点后要写入的位数,尽管这会随着不同的转换而变化.
指示如何格式化参数的转换字符. d是十进制整数,f是浮点的十进制格式,s在我们的例子中不改变字符串.
更多信息可以在here.找到
内容总结
以上是互联网集市为您收集整理的如何在Java中正确使用printf格式化全部内容,希望文章能够帮你解决如何在Java中正确使用printf格式化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。