Read
Scanner
数据量小的时候推荐
头文件:
1
| import java.util.Scanner;
|
样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import java.util.Scanner; public class Main { Scanner cin = new Scanner(System.in); String s = cin.next(); String s1 = cin.nextLine(); int a = cin.nextInt(); long b = cin.nextLong(); boolean c = cin.nextBoolean(); double d = cin.nextDouble(); float f = cin.nextFloat(); byte e = cin.nextByte(); }
|
BufferedReader
读取字符串时推荐,数据量大的时候性能比Scanner好很多
头文件:
样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import java.io.*;
public class Main { public static void main(String[] args)throws Exception{ BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); String[] words = cin.readLine().split(" "); String s = words[0];
int a = Integer.parseInt(cin.readLine());
String s1 = cin.readLine(); } }
|
StreamTokenizer
读取整数、小数时推荐
头文件:
样例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import java.io.*;
public class Main { public static void main(String[] args)throws Exception{ StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); cin.nextToken(); int a = (int)cin.nval; cin.nextToken(); int b = (int)cin.nval;
cin.nextToken(); double d = cin.nval; } }
|