king_wangyao 发表于 2013-2-1 12:33:49

从键盘上获取名字、语文、数学的成绩,计算总分然后填充到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;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;    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;    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;      System.out.println(stu.name);      stu.record = Integer.parseInt(str);      System.out.println(stu.record);      stu.record = Integer.parseInt(str);      stu.record = Integer.parseInt(str);      stu.record = Integer.parseInt(str);      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 + " " + stud.record + " "          + stud.record + " " + stud.record + " " + 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;    FileInputStream fis = new FileInputStream("C:\\Documents and Settings\\tliu\\桌面\\新建文件夹\\xi.txt");    ObjectInputStream ois = new ObjectInputStream(fis);    Students[] students = new Students;    for (int i = 1; i < d; i++) {      students = (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.avg > students.avg)          max = j;      }      System.out.println(students.name + "   " + students.record + "" + students.record + ""          + students.record + "" + students.record + "" + students.total + "" + students.avg          + "" + students.grade + "" + i);    }}}
页: [1]
查看完整版本: 从键盘上获取名字、语文、数学的成绩,计算总分然后填充到Student对象中,并将对象写入文件