重定向文件实例

作者在 2010-05-21 15:26:41 发布以下内容
import java.io.*;

public class TestSetInput{
    public static void main(String[] args){
        try{
            //FileInputStream fis = new FileInputStream("source.txt");
            
//System.setIn(fis);       如果取消了这里的,将在下面的System.io是文件中的内容     
            int avg = 0;
            int total = 0;
            int count = 0;
            int num = 0;
            int i;
            BufferedReader br = new BufferedReader(new InputStreamReader

(System.in));
            String s = br.readLine();//以传统的方法读取
            while(s != null && !s.equals("over")){
                i = Integer.parseInt(s);
                num++;
                total += i;
                avg = total/num;
                System.out.println("num=" + num + "\ttotal=" + total +

"\tavg=" + avg);    
                s = br.readLine();
            }
        }catch(Exception e){
            e.printStackTrace();    
        }
    }
}
I/O编 | 阅读 1022 次
文章评论,共0条
游客请输入验证码
浏览290929次