从键盘上获取名字、语文、数学的成绩,计算总分然后填充到Student对象中,并将对象写入文件
下面是源文件
Students.java文件
package com.kingsoft.main;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Students implements Serializable { String name; int[] record = new int[4]; int total; float avg; String grade; public Students() { } public String getName() { return name; } public int[] getRecord() { return record; } public int getTotal() { int s = 0; for (int i = 0; i < record.length; i++) s += record[i]; return s; } public float getAvg() { float f = getTotal() / 4f; return f; } public String getGrade() { String str; if (avg < 100 && avg > 90) str = "A"; else if (avg > 80) str = "B"; else if (avg > 70) str = "C"; else if (avg > 60) str = "D"; else str = "F"; return str; }}Students1.java文件
package com.kingsoft.main;import java.io.*;public class Students1 { public static int s = 0; /** Creates a new instance of Students1 */ public Students1() { } public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.println("请顺序输入名字及国语,英语,数学,科学的成绩.(eof:输入完毕)"); String data; String[] str = new String[5]; FileOutputStream fos = new FileOutputStream("C:\\Documents and Settings\\tliu\\桌面\\新建文件夹\\xi.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); while (true) { data = br.readLine(); s++; if (data.equals("eof")) break; str = data.split(" "); Students stu = new Students(); stu.name = str[0]; System.out.println(stu.name); stu.record[0] = Integer.parseInt(str[1]); System.out.println(stu.record[0]); stu.record[1] = Integer.parseInt(str[2]); stu.record[2] = Integer.parseInt(str[3]); stu.record[3] = Integer.parseInt(str[4]); stu.total = stu.getTotal(); stu.avg = stu.getAvg(); stu.grade = stu.getGrade(); oos.writeObject(stu); } oos.close(); System.out.println("文件内容"); FileInputStream fis = new FileInputStream("D:\\xi.txt"); ObjectInputStream ois = new ObjectInputStream(fis); for (int j = 1; j < s; j++) { Students stud; stud = (Students) ois.readObject(); System.out.println("Students对象" + j + " " + "{" + stud.name + " " + stud.record[0] + " " + stud.record[1] + " " + stud.record[2] + " " + stud.record[3] + " " + stud.total + " " + stud.avg + " " + stud.grade + "}"); } ois.close(); }}class Students2 { public Students2() { } public static void main(String[] args) throws Exception { // TODO code application logic here int d = Students1.s; int[] a = new int[d]; FileInputStream fis = new FileInputStream("C:\\Documents and Settings\\tliu\\桌面\\新建文件夹\\xi.txt"); ObjectInputStream ois = new ObjectInputStream(fis); Students[] students = new Students[d]; for (int i = 1; i < d; i++) { students[i] = (Students) ois.readObject(); } ois.close(); System.out.println("名字 " + "国语 " + "英语 " + "数学 " + "科学 " + "总分 " + "平均分 " + "学分 " + "顺序"); for (int i = 1; i < d; i++) { int max = i; for (int j = i; j < d; j++) { if (students[j].avg > students[i].avg) max = j; } System.out.println(students[max].name + " " + students[max].record[0] + " " + students[max].record[1] + " " + students[max].record[2] + " " + students[max].record[3] + " " + students[max].total + " " + students[max].avg + " " + students[max].grade + " " + i); } }}