繁体   English   中英

使用String.formatter分隔到不同的行

[英]Using String.formatter to separate onto different lines

return String.format( "Name:\t%s", "\nHeight:", "\nWeight:",  name);

首先,为什么根本没有出现“ Weight:”? 本质上,我需要打印以下内容:

姓名:约翰·史密斯

高度:1.6m

体重:65kg

身高和体重存储在私有字段中。

如何使用String.formatter构造此字符串? 我尝试了各种方法,但似乎没有达到预期的效果。

有人可以帮我建造这个吗?

第一个String是“格式”,其他参数是您要在格式内替换的值

例如...

System.out.println(String.format( "Name:\t%s%nHeight:\t%.1fm%nWeight:\t%.1fkg", "Bruce", 1.6, 65.0));

打印...

Name:   Bruce
Height: 1.6m
Weight: 65.0kg

查看本教程String#format JavaDocs了解更多详细信息

return String.format("Name:\t%s\nHeight:\t%s\nWeight:\t%s", name, height, weight);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM