forked from algorithmzuo/algorithm-journey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode02_InputLong.java
More file actions
52 lines (46 loc) · 1.63 KB
/
Code02_InputLong.java
File metadata and controls
52 lines (46 loc) · 1.63 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package class097;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
// 读入long类型数字的注意点
// 讲解019的扩展,没有看过讲解019的同学去看一下
public class Code02_InputLong {
public static void main(String[] args) throws IOException {
f1();
f2();
}
public static void f1() throws IOException {
System.out.println("f1函数测试读入");
// 尝试读入 : 131237128371723187
// in.nval读出的是double类型
// double类型64位
// long类型也是64位
// double的64位会分配若干位去表达小数部分
// long类型的64位全用来表达整数部分
// 所以读入是long范围的数,如果用以下的写法
// in.nval会先变成double类型,如果再转成long类型,就可能有精度损耗
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
long num = (long) in.nval;
out.println(num);
out.flush();
}
public static void f2() throws IOException {
System.out.println("f2函数测试读入");
// 尝试读入 : 131237128371723187
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// 直接读出字符串
String str = br.readLine();
// 然后把字符串转成long
// 不可能有精度损耗
long num = Long.valueOf(str);
out.println(num);
out.flush();
}
}