提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用String format()方法生成包含动态数据占位符的String。
public class Main { /* 来自 *时代Java - N o w J a v a . c o m*/ public static void main(String[] args){ double temperature = 8.6; String temperatureString = "The current temperature is %.1f degrees Fahrenheit."; String newString = String.format(temperatureString, temperature); System.out.println(newString); temperature = 101.2; System.out.println(String.format(temperatureString, temperature)); } }