Java算法-快读IO类实现(替代Scanner,有hasNext())

该代码片段展示了一个Java程序模板,主要涉及FastReader类的使用,用于高效读取输入数据。模板中还包括基础的输入输出处理,如静态变量定义、问题解决方法以及Scanner的封装。FastReader类是基于Scanner优化的,能更快地读取输入。
摘要由CSDN通过智能技术生成
```java
import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
    /** 输出流, 输出完之后记得用 out.flush() 清空一下缓存区 */
    static PrintWriter out = new PrintWriter(System.out);
    /** FastReader类  */
    static FastReader sc = new FastReader();

    ///
    静态变量///

    // static final int N = (int) 1e5 + 10, INF = 0x3f3f3f3f;
    // static int arr[] = new int[N];
    // static int q[] = new int[N];
    // static char g[][] = new char[N][];
    // static boolean st[][] = new boolean[N][N];
    static int n, m, t;
    static int res, ans;
    ///
    静态方法///

    ///
    static void solve() {
        while (sc.hasNext()) {
            $END$
                    
                    
            out.flush();
        }
        out.flush();
    }
    ///

    ///
    //写解决方法/

    static void solveCom() { // 不需要 hasNext()
        int n = sc.nextInt();

        
        out.flush();
    }


    static void solveSub() { // t 次
        t = sc.nextInt();
        while (t-- > 0) {

            
            out.flush();
        }
        out.flush();
    }

    static void solveScan() { // 需要 hasNext()
        solve();
        
        
        out.flush();
    }
    ///

    ///

    public static void main(String[] args) throws Exception {
        /* 调用solve方法,好处是有多个题解可以写n个solve方法 */
        // solveCom();
        // solveSub();
        solveScan();

        // 最后清空并关闭输出流
        out.flush();
        out.close();
    }

    //封装快速读取类的方法(基于Scanner的方法来写)/
    static class FastReader { // 输入类
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        public FastReader() {    }

        String next () {
            while (st == null || !st.hasMoreElements()) {
                try {  st = new StringTokenizer(br.readLine());  }
                catch (IOException e) {  e.printStackTrace();  }
            }
            return st.nextToken();
        }
        boolean hasNext() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    String line = br.readLine();
                    if (line == null) return false;
                    st = new StringTokenizer(line);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return true;

        }

        int nextInt () {  return Integer.parseInt(next());  }

        long nextLong () {  return Long.parseLong(next());  }

        double nextDouble () {  return Double.parseDouble(next());  }

        String nextLine () {
            String line = null;
            try {  line = br.readLine();  }
            catch (IOException e) {  e.printStackTrace();  }
            return line;
        }
    }
    ///
}

```

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值