Java程序设计教程(第三版) 雍俊海 7.3 答案/代码

时间:2021-10-05 11:29:00



哎,为了期末考试,每个都试了一下,也不知道对不对、、、


题目:

编写一个程序。要求输入5个学生的成绩(从0到100的整数),并将这5个数保存到文件“data.txt” 中。

然后再编写一个程序,从文件“data.txt” 中读取这5个学生的成绩,

计算并输出它们的平均数,然后再按从大到小的顺序输出5个学生的成绩




代码:


Scanner+FileOutputStream+FileInputStream


import java.io.*;
import java.util.*;

public class test1_7 {

	//Scanner+FileOutputStream+FileInputStream
	public static void main(String args[]) throws IOException
	{
		int cur_Grade;
		Scanner sc = new Scanner(System.in);
		try 
		{
			OutputStream fos = new FileOutputStream(new File("data.txt"));
			for(int i=0;i<5;i++)
			{
				cur_Grade = sc.nextInt();
				fos.write(cur_Grade);
			}
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		sc.close();
		
		int[] Grades = new int[10];
		int i =0;
		double sum = 0;
		int readnum;
		InputStream fis = new FileInputStream(new File("data.txt"));
		while((readnum = fis.read())!=-1)
		{
			Grades[i++] = readnum;
		}
		
		fis.close();
		
		Arrays.sort(Grades,0,5);
		for(i=0;i<5;i++)
		{	
			sum += Grades[i];
			System.out.print(Grades[i]+" ");
		}
		System.out.println("\n平均数为:"+sum/5);
	}

}


DataOutputStream+BufferedReader


import java.io.*;
import java.util.*;

public class test1_7 {

	//DataOutputStream+BufferedReader
	public static void main(String args[]){
		try{
			DataInputStream d_is = new DataInputStream(System.in);
			FileOutputStream f_os = new FileOutputStream("data.txt");
			DataOutputStream d_os = new DataOutputStream(f_os);
			
			for(int i=0 ; i<5 ; i++){
				int n = d_is.readInt();
				d_os.writeInt(n);
			}
			
			int n = 5;
			double score[] = new double[5];
			String line;
			
			FileReader fr = new FileReader("data.txt");
			BufferedReader i_br = new BufferedReader(fr);
			
			int row = 0;
			
			while((line = i_br.readLine())!=null){
				score[row++] = Double.parseDouble(line);
			}
			
			i_br.close();
			fr.close();
			
			Arrays.sort(score);
			
			for(int i=0 ; i


BufferedReader+BufferedWriter


import java.io.*;
import java.util.*;

public class test1_7 {

	
	//BufferedReader+BufferedWriter
	public static void main(String args[]){
		try{
			int n = 5;
			double score[] = new double[5];
			String line;
			
			InputStreamReader i_sr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(i_sr);
			//BufferedWrtier不要写错了,这里是没有File的!
			FileWriter fw = new FileWriter("file1.txt");
			BufferedWriter b_fw = new BufferedWriter(fw);
			
			System.out.println("请输入学生成绩:");
			
			for(int i=0 ; i


PrintStream+BufferedReader


import java.io.*;
import java.util.*;

public class test1_7 {
	//PrintStream+BufferedReader
	public static void main(String args[]){
		try{
			Scanner scan = new Scanner(System.in);
			File file = new File("file2.txt");
			FileOutputStream f_os = new FileOutputStream(file);
			PrintStream ps = new PrintStream(f_os);
			
			int ch;
			for( int i=0 ; i<5 ; i++ ){
				ch = scan.nextInt();
				ps.println(ch + "");
			}
			
			ps.close();
			
			int n = 5;
			double score[] = new double[5];
			String line;
			
			FileReader fr = new FileReader("file2.txt");
			BufferedReader i_br = new BufferedReader(fr);
			
			int row = 0;
			
			while((line = i_br.readLine())!=null){
				score[row++] = Double.parseDouble(line);
			}
			
			i_br.close();
			fr.close();
			
			Arrays.sort(score);
			
			for(int i=0 ; i



有错的话,还望纠正一下,感觉还是很多问题没有解决的