gpt4 book ai didi

java - 使用java的printf方法打印问题

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

我正在尝试使用 printf 打印百分号。当我使用以下内容时:

System.out.printf("%-3d%%\n",10);
System.out.printf("%-3d%%\n",100);
System.out.printf("%-3d%%\n",0);

我最终得到:

10 %  
100%
0 %

如何实现以下目标

10%   - 1 space after % sign
100% - no space
0% - 2 spaces after % sign

有什么想法吗?

最佳答案

尝试

    System.out.printf("%-4.4s\n",10 + "%");
System.out.printf("%-4.4s\n",100+ "%");
System.out.printf("%-4.4s\n",0+ "%");

这可以通过以下方式验证

    System.out.printf("%-4.4s|\n",10 + "%");
System.out.printf("%-4.4s|\n",100 + "%");
System.out.printf("%-4.4s|\n",0+ "%");

产生

10% |
100%|
0% |

关于java - 使用java的printf方法打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23335767/

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