ch8ex10.java

来自「JAVA程序设计 丁岳伟 彭敦陆编 高等教育出版社 第7---11章程序」· Java 代码 · 共 59 行

JAVA
59
字号
import java.io.*;
public class ch8ex10{
	public static void main(String[]args){
		int x;
		try{
			DataOutputStream fout=new DataOutputStream(new FileOutputStream("grades.dat",true));
			for(int j=0;j<200;j++){
				x=(int)(Math.random()*1000000)%100;
				fout.writeInt(x);
			}
			fout.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		int fail=0;	int pass=0;
		int levelA=0;int levelB=0;
		int levelC=0;
		try{
			DataInputStream fin=new DataInputStream(new FileInputStream("grades.dat"));
			while(fin.available()>0){
				x=fin.readInt();
				if(x<60) fail++;
				else if(x>=60&&x<70) pass++;
					else if(x>=70&&x<80) levelC++;
						else if(x>=80&&x<90) levelB++;
							else levelA++;
			}
			fin.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println("Class A level:  "+levelA);
		System.out.println("Class B level:  "+levelB);
		System.out.println("Class C level:  "+levelC);
		System.out.println("Pass:  "+pass);
		System.out.println("Fail:  "+fail);
		try{
			DataInputStream fin=new DataInputStream(new FileInputStream("grades.dat"));
			DataOutputStream fout1=new DataOutputStream(new FileOutputStream("fails.dat"));
			DataOutputStream fout2=new DataOutputStream(new FileOutputStream("temp.dat"));
			while(fin.available()>0){
				x=fin.readInt();
				if(x<60)
					fout1.writeInt(x);
				else
					fout2.writeInt(x);
			}
			fin.close();
			fout1.close();
			fout2.close();
			File oldFile=new File("grades.dat");
			oldFile.delete();
			File newFile=new File("temp.dat");
			newFile.renameTo(oldFile);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?