gpt4 book ai didi

java - Java 中的 printf() 结构

转载 作者:行者123 更新时间:2023-12-01 11:08:08 26 4
gpt4 key购买 nike

我正在使用 printf 构建一个包含三列的表格,但遇到了一些麻烦。

我目前正在使用:

System.out.printf("%-10s %33s %5.2f\n", "New Price:", "$", newPrice);
System.out.printf("%-10s %28s %1.2f\n", "Depreciation Amount:", "$", -Dep1);
System.out.printf("%-10s %23s %3.2f\n", "Other Depreciation Amount:", "$", -Dep2);
System.out.printf("%-10s %21s %5.2f\n\n", "Current Market Value:", "$", curVal);
System.out.printf("%-10s %32s %6.2f\n\n", "Other Amount:", "$", otherAmount);

这给了我一个输出:

New Purchase Price:                                 $ 60000.00
Age Depreciation Amount: $ -18000.00
Distance Depreciation Amount: $ -9600.00
Estimated Current Market Value: $ 32400.00

Base Premium Amount: $ 1620.00

我不知道如何调整 printf 使其显示为:

New Purchase Price:                                 $  60000.00
Age Depreciation Amount: $ -18000.00
Distance Depreciation Amount: $ -9600.00
Estimated Current Market Value: $ 32400.00

Base Premium Amount: $ 1620.00

我如何实现这一目标?

干杯

最佳答案

方法如下:

System.out.printf("%-30s %2s %8.2f\n", "New Price:", "$", newPrice);
System.out.printf("%-30s %2s %8.2f\n", "Depreciation Amount:", "$",
-Dep1);
System.out.printf("%-30s %2s %8.2f\n", "Other Depreciation Amount:",
"$", -Dep2);
System.out.printf("%-30s %2s %8.2f\n\n", "Current Market Value:", "$",
curVal);
System.out.printf("%-30s %2s %8.2f\n\n", "Other Amount:", "$",
otherAmount);

1) 宽度是总宽度,包括句点和分数。

2)不要手动计算常量的宽度,计算机更擅长。

3)将来,发布与结果相对应的实际代码,而不是近似值。

关于java - Java 中的 printf() 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32712258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com