一、快讀-BufferedReader實現?
為了方便,自己寫了個類
注意:BufferedReader需要拋異常(throws IOException)
class in
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
// nextLine()讀取字符串
static String nextLine() throws IOException { return reader.readLine(); }
// next()讀取字符串
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
// 讀取一個int型數值
static int nextInt() throws IOException { return Integer.parseInt(next()); }
// 讀取一個double型數值
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
// 讀取一個long型數值
static long nextLong() throws IOException { return Long.parseLong(next());}
// 讀取一個BigInteger
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(in.nextLine());
return d;
}
}
二、快寫-PrintWriter實現
注意:
1、PrintWriter也需要拋異常(throws IOException)
2、需要手動刷洗緩存區,要不然不輸出(pw.flush();)
三、實操代碼
package hly;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
class in
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
// nextLine()讀取字符串
static String nextLine() throws IOException { return reader.readLine(); }
// next()讀取字符串
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
// 讀取一個int型數值
static int nextInt() throws IOException { return Integer.parseInt(next()); }
// 讀取一個double型數值
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
// 讀取一個long型數值
static long nextLong() throws IOException { return Long.parseLong(next());}
// 讀取一個BigInteger
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(in.nextLine());
return d;
}
}
public class Main
{
public static void main(String[] args) throws IOException
{
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 讀入int
int a = in.nextInt();
pw.println(a);
// 讀入long
long b = in.nextLong();
pw.println(b);
// 讀入double
double c = in.nextDouble();
pw.println(c);
// 讀入字符串
// nextLine方式輸入的字符串可以含有空格(空格字符也會存儲在字符串里面),也就是說它是以回車作為輸入的結束符。
String s1 = in.nextLine();
pw.println(s1);
// 讀入字符串
// next輸入的字符串不能含有空格,它以空格、tab、回車作為輸入的結束符。
String s2 = in.next();
pw.println(s2);
// 讀入BigInteger
BigInteger d = in.nextBigInteger();
pw.append("" + d);
pw.flush();
}
}
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧