如果不使用格式化输出,就需要进行字符串连接,如果变量比较多,拼接就会显得繁琐
使用格式化输出,就可以简洁明了
%s 表示字符串
%d 表示数字
%n 表示换行
示例 : 格式化输出
public class Sder {
public static void main(String[] args) {
String name ="林某某";
int kill = 8;
String title="坑she";
//直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
String sentence = name+ " 在进行了连续 " + kill + " 次被击杀后,获得了 " + title +" 的称号";
System.out.println(sentence);
//使用格式化输出
//%s表示字符串,%d表示数字,%n表示换行
String sentenceFormat ="%s 在进行了连续 %d 次被击杀后,获得了 %s 的称号%n";
System.out.printf(sentenceFormat,name,kill,title);
}
}
示例 : printf和format
public class Sder {
public static void main(String[] args) {
String name ="德某";
int kill = 99;
String title="超diao神";
String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
//使用printf格式化输出
System.out.printf(sentenceFormat,name,kill,title);
//使用format格式化输出
System.out.format(sentenceFormat,name,kill,title);
}
}
示例 : 总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
import java.util.Locale;
public class Sder {
public static void main(String[] args) {
int year = 2020;
//总长度,左对齐,补0,千位分隔符,小数点位数,本地化表达
//直接打印数字
System.out.format("%d%n",year);
//总长度是8,默认右对齐
System.out.format("%8d%n",year);
//总长度是8,左对齐
System.out.format("%-8d%n",year);
//总长度是8,不够补0
System.out.format("%08d%n",year);
//千位分隔符
System.out.format("%,8d%n",year*10000);
//小数点位数
System.out.format("%.2f%n",Math.PI);
//不同国家的千位分隔符
System.out.format(Locale.FRANCE,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.US,"%,.2f%n",Math.PI*10000);
System.out.format(Locale.UK,"%,.2f%n",Math.PI*10000);
}
}