Java中创建格式化字符串
第一种:
public class Main{
public static void main(String[] args) {
float floatVar = 3.2f;
int intVar = 20;
String stringVar = "爷";
System.out.printf("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
"is %s", floatVar, intVar, stringVar);
}
}
第二种:
public class Main{
public static void main(String[] args) {
float floatVar = 3.2f;
int intVar = 20;
String stringVar = "爷";
String fs;
fs = String.format("浮点型变量的值为 " +
"%f, 整型变量的值为 " +
" %d, 字符串变量的值为 " +
" %s", floatVar, intVar, stringVar);
System.out.println("" +fs);
}
}
输出结果:
记得float、long、double定义变量时要加后缀