Core Java (三) Java的输入与输出

释放双眼,带上耳机,听听看~!

一个简单的java输入输出实例,包含了最常用的输入输出函数。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1package com.xujin;
2
3import java.util.Date;
4import java.util.Scanner;
5
6public class MyTest{
7   static Scanner cin = new Scanner(System.in);
8   public static void main(String[] args){
9      
10      System.out.print("input your name:");
11      String name = cin.next();//读入下一个单词,以空格作为标准
12      System.out.print("input your age:");
13      int age = cin.nextInt();//读入下一个int,以空格作为标准
14      System.out.print("input your salary:");
15      double salary = cin.nextDouble();
16      System.out.print("input detail:");
17      String detail = cin.nextLine();//读入下一行,这里是读入上一次的回车
18      detail = cin.nextLine();//读入下一行
19     
20      //格式化输出
21      System.out.printf("Name:%s\n", name);
22      System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20  Oct:24  Hex:0x14
23      System.out.printf("Salary:%#,8.2f\nDetail:",salary);
24      System.out.println(detail);
25     
26      //时间输出
27      System.out.printf("Now:%tc\n",new Date());//output:星期三 一月 16 21:02:10 CST 2013
28      System.out.printf("%1$s %2$tY %2$tB %2$td\n", "Now:",new Date());//output:2013 一月 16
29      System.out.printf("%s %tY %<tB %<td\n", "Now:",new Date());//output:2013 一月 16
30     
31      //使用静态的String.format方法常见一个格式化的字符串,而不打印输出
32      System.out.println(detail);
33      String message = String.format("Hello,%s! You will be %d next year", name, ++age);
34      System.out.print(message);
35  }
36}
37

console结果:

input your name:Gin
input your age:20
input your salary:5000
input detail:detail…
Name:Gin
Age:20
Oct:24
Hex:0x14
Salary:5,000.00
Detail:detail…
Now:星期三 一月 16 21:12:38 CST 2013
Now: 2013 一月 16
Now: 2013 一月 16
detail…

Hello,Gin! You will be 21 next year

解释:


1
2
1System.out.printf("Age:%d\tOct:%<o\tHex:%1$#x\n",age);//output:Age:20 Oct:24  Hex:0x14
2

%<o表示格式化前面说明的数值,以八进制显示age
%1$#x表示格式化索引为1(1$,第一个参数,从1开始)的数值,并填加前缀0x或0(#对x或o)


1
2
1System.out.printf(&quot;Salary:%#,8.2f\nDetail:&quot;,salary);
2

1
2
1包含小数点(\#对f),8.2表示用8个字符宽度和小数点后两个字符的精度打印  
2

Java的格式输出,,在%后面使用

转换符 类型
d 十进制整数
x 十六进制整数
o 八进制整数
f 定点浮点数
e 指数浮点数
g 通用浮点数
a 十六进制浮点数
s 字符串
c 字符
b 布尔
h 散列码
tx 日期时间
% 百分号
n 与平台有关的行分隔符

1
1

用于printf的标志,在%后面使用

标志 目的 举例
+ 打印数字前的符号 +3333.33
space  在正数之前加空格 |  3333.33|
0 在数字前补0 003333.33
 左对齐 |3333.33  |
(  负数括在括号内 (3333.33)
 ,  添加分组分隔符 3,333.33
# (对于 f )  包含小数点  3,333.
# (for x or o)  添加前缀 0x 或 0  0xcafe
转化为大写  0XCAFE
$   指定格式化参数索引,如%1$d,%1$d表示以十进制和十六进制打印第一个参数 159 9F
格式化前面参数,如%d%<x表示以十进制和十六进制打印同一个参数 159 9F

1
1

时间日期的转换符:

    转换符  类型 举例

C 完整日期和时间 Mon Feb 09 18:05:19 PST 2004

F ISO 8601 日期 2004-02-09

D 美国时间格式 (mm/dd/year) 02/09/2004

T 24小时时间 18:05:19

r 12小时时间 06:05:19 pm

R 24小时无秒时间 18:05

Y 四位年 2004

y 年的后两位 04

C 年的前两位 20

B 月的完整拼写 February

b or h  月的缩写 Feb

m 两位月(前补0) 02

d 两位日(前补0) 09

e 日期(前不补0) 9

A 完整星期几 Monday

a 星期几的缩写 Mon

j 这一年的第多少天,三位补0 069

H 24小时制小时,两位补0 18

k 24小时制小时,两位不补0 18

I 12小时制小时,两位补0 06

l 12小时制小时,两位不补0 6

M 分钟,两位补0 05

S 秒,两位补0 19

L 毫秒,三位补0 047

N 毫微秒,九位补0 047000000

P 上下午大写 PM

p 上下午小写 pm

z RFC 822 numeric offset from GMT -0800

Z 时区 PST

s 1970-01-01 00:00:00起秒数 1078884319

E 1970-01-01 00:00:00起毫秒数  1078884319047

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列 Crypto加密模块

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索