输入与输出
1.输入
Scanner sc=new Scanner(System.in);
Scanner in = new Scanner (new BufferedInputStream(System.in));
1. 只有一组数据:
Scanner sc=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
2. 输入多组数据,不告知组数,也没有截止符
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=s.nextInt();
int b=s.nextInt();
}
3. 输入多组数据,不告知组数,截止符为ch
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int c=sc.nextInt();
if(c==ch) return;
}
4. 输入多组数据,第一行是整数N,表示有N组数据,之后每行表示一组数据
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
for(int i=0;i<n;i++){
int a=sc.nextInt();
int b=sc.nextInt();
}
5.养成良好习惯,最后关闭输入流
sc.close();
- 输入挂(当输入规模达到106的时候,就需要输入挂,否则很有可能超时)
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
out.close();
}
}
class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return