System.out.flush()没有刷新stdout缓冲区

时间:2015-06-27 10:54:51

标签: java writing

我的计算nCrTable的代码即使在包含stdout之后也不会刷新System.out.flush(),是否存在刷新问题或其他问题?

n<478时提供正确的输出。使用System.out.println()解决了这个问题,但我不希望换行n的新行。

package nCrTable;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class solution {

     public static void main(String args[]) throws IOException{
        Scanner sc=new Scanner(System.in);
        int count=sc.nextInt();
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
        for(int i=0;i<count;i++){
            makenCr(sc.nextInt(),bw);
        }
        sc.close();
    }
    public static void makenCr(int n,BufferedWriter bw) throws IOException{

        int nCr[]=new int[(n+2)/2];
        long amount=1;
        nCr[0]=1;
        int no=1000000000;
        System.out.print(nCr[0]+" ");
        System.out.flush();
        int i;
        for( i=1;i<(n+2)/2;i++){
            nCr[i]=(int)((amount*(n-i+1)/i)% no);
            amount=nCr[i];
            System.out.print(nCr[i]+" ");
            System.out.flush();
        }
        while(i<n+1){
            System.out.print(nCr[n-i]+" ");
            System.out.flush();
            i++;
        }
        System.out.println();
    }
}

0 个答案:

没有答案