输入与输出
输入
获取用键盘输入常用的两种方法
方法 1:通过 Scanner
Scanner input = new Scanner(System.in);
String s = input.nextLine();
input.close();
方法 2:通过 BufferedReader
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String s = input.readLine();
输出
System.out.println("字符串");//换行打印
System.out.print("\n");//换行打印
System.out.print("字符串");//换行打印
System.out.print(a);//不换行打印变量
System.out.print("字符串"+a);//混合输出字符串和变量值
System.out.printf("%");//按格式输出
printf 格式化输出符号详细说明_xiexievv的专栏-CSDN博客_printf格式化输出
格式化输入与输出
%
Converter | Flag | Explanation |
---|---|---|
d | A decimal integer. | |
f | A float. | |
n | A new line character appropriate to the platform running the application. You should always use %n , rather than \n . | |
tB | A date & time conversion—locale-specific full name of month. | |
td, te | A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not. | |
ty, tY | A date & time conversion—ty = 2-digit year, tY = 4-digit year. | |
tl | A date & time conversion—hour in 12-hour clock. | |
tM | A date & time conversion—minutes in 2 digits, with leading zeroes as necessary. | |
tp | A date & time conversion—locale-specific am/pm (lower case). | |
tm | A date & time conversion—months in 2 digits, with leading zeroes as necessary. | |
tD | A date & time conversion—date as %tm%td%ty | |
08 | Eight characters in width, with leading zeroes as necessary. | |
+ | Includes sign, whether positive or negative. | |
, | Includes locale-specific grouping characters. | |
- | Left-justified… | |
.3 | Three places after decimal point. | |
10.3 | Ten characters in width, right justified, with three places after decimal point. |
float a=0.23456f;
System.out.format("a保留3位小数:%.3f",a);//a保留3位小数:0.235