java

当前位置:中华考试网 >> java >> java基础 >> 文章内容

java入门教程:创建格式化字符串

来源:中华考试网  [2016年4月27日]  【

创建格式化字符串

  我们知道输出格式化数字可以使用printf()和format()方法。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);

责编:ljnbset
  • 会计考试
  • 建筑工程
  • 职业资格
  • 医药考试
  • 外语考试
  • 学历考试