printf在java里语法_如何在Java中正确使用printf格式化

我正在尝试创建一个简单的程序来接收三个项目,它们的数量和价格,并将它们全部加在一起以创建一个简单的收据类型格式.我的教授给了我一个特定的收据格式,其中所有小数都排成一行并且一致地放置.它看起来应该是这样的.

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("

The 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("

The 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("

The 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("

Your bill: ");

System.out.printf("

Item");

System.out.printf("%30s", amount);

// System.out.printf("

%s %25d %16.2f %11.2f", item, quantity, price,

// total);

// System.out.printf("

%s %25d %16.2f %11.2f", item2,quantity2, price2,

// total2);

// System.out.printf("

%s %25d %16.2f %11.2f", item3,quantity3, price3,

// total3);

System.out.printf("

%s", item);

System.out.printf("%30d", quantity);

System.out.printf("

%s", item2);

System.out.printf("

%s", item3);

System.out.printf("

Subtotal %47.2f", grandTotal);

System.out.printf("

6.25 %s sales tax %39.2f", taxSign, salesTax);

System.out.printf("

Total %50.2f", grandTotalTaxed);

}

}

如果我输入更长的商品名称,则会移动数量,价格和总数的位置.

我的问题是,如何使用printf创建一个有限宽度的设置起点,请帮忙.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值