类String有一个等效的类方法 ,format()它返回一个String对象而不是一个PrintStream对象。
使用String的静态format()方法允许您创建可以重用的格式化字符串,而不是一次性打印语句。
例如:
System.out.printf("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar);
你可以写:
String fs; fs = String.format("The value of the float " + "variable is %f, while " + "the value of the " + "integer variable is %d, " + " and the string is %s", floatVar, intVar, stringVar); System.out.println(fs);