Java 更快的输入输出方法

定义输入和输出

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

输入方式

in.nextToken();
int n = (int) in.nval;//相当于int n = in.nextInt();

输出方式

out.println("HelloWorld!");
out.printf("HelloWorld!");
out.print("HelloWorld!");

题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/3004/pid/2736

完整用法:

/*导包*/
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));
		in.nextToken();
		int n = (int) in.nval;
		int sum1=0;
		int sum2=0;
		for(int i=0;i<n;i++)
		{
			in.nextToken();
			int d=(int)in.nval;
			if(i%2==0)
			{
				sum1+=d;
			}
			else
			{
				sum2+=d;
			}
		}
		if(sum1>sum2)
		{
			out.println("Greater than");
		}
		else if(sum1<sum2)
		{
			out.println("Less than");
		}
		else
			out.println("Equal");
		out.flush();
	}
}

你可能感兴趣的:(java)