Java快速输入输出

需要的头文件有

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

在主函数中使用StreamTokenizer和PrintWriter类

StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));

输入结束标志参数为
StreamTokenizer.TT_EOF
注意读入的时候要调用nextToken()函数,输出之后调用flush()函数,以hdu 1029为例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
        while(in.nextToken()!=StreamTokenizer.TT_EOF) {
            int n=(int)in.nval;//读入一个整数
            int []c=new int[100000];
            int ans=0;
            for(int i=0;i<n;++i) {
                in.nextToken();
                int x=(int)in.nval;
                ++c[x];
                if(c[x]>=(n+1)/2) ans=x;
            }
            out.println(ans);
            out.flush();//输出一次之后要刷新
        }
    }
}

二、BufferedReader实现快速输入读一行

需要导入的jar

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
BufferedReader inBuff=new BufferedReader(new InputStreamReader(System.in));
BufferedReader inBuff=new BufferedReader(new InputStreamReader(System.in));
String s=inBuff.readLine();
System.out.println("有问题吗"+s+"没有吧");
posted @ 2020-05-10 21:21  Garrett_Wale  阅读(297)  评论(0编辑  收藏  举报