1-输出格式化
package digit;
public class TestNumber {
public static void main(String[] args) {
String name ="盖伦";
int kill = 8;
String title="超神";
//直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
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);
System.out.format(sentenceFormat,name,kill,title);
}
}
2-换行符\n:另起一行与回车符\r:回到开头
在eclipse里敲一个回车,实际上是回车换行符
在DOS和Windows中,每行结尾是 “\r\n”;
System.out.printf(
"这是换行符%n"
);
3-关于print
int i = 4;
double j = 5;
System.out.print("用print输出i:"+ i);
System.out.println( "用println输出i:"+ i);
System.out.printf("i的值为%d,j的值为%f", i,j);
用print输出i:4用println输出i:4(换行了)
i的值为4,j的值为5.000000(格式输出)
4-各种格式化小方法
package digit;
import java.util.Locale;
public class TestNumber {
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);
}
}