gpt4 book ai didi

java - 串连

转载 作者:行者123 更新时间:2023-12-02 01:17:00 24 4
gpt4 key购买 nike

如果我希望输出字符串长度至少为 5 位数字,我可以使用例如 System.out.printf("'%5d'", 10) ,但是我可以用一些包含字符串长度的变量替换 5 吗?或者我应该使用什么才能使我的输出与 X 一样长?

最佳答案

当然。只需在格式字符串中使用变量即可:

int length = 50;
System.out.printf("'%"+length+"d'", 10);

输出:

'                                                10'

关于java - 串连,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413259/

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