想编写一个用来输入全班40名考生学号和名字,还有三门(数学,英语,计算机)课程成绩,计算每位同学3门课程成绩的平均分,然后把它输入到文件score.txt文件中,我写了下,可是编译的时候遇到问题,请大家指导下哦,谢谢了!import java.io.*;
class InputDate
{
static private String s="";
static public void input()
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
try{
s=br.readLine();
}
catch(IOException e){}
  }
static public long getLong()
{
input();
return Long.parseLong(s);
}
static public float getFloat()
{
input();
return Float.parseFloat(s);
}
static public String getString()
{
input();
return s;
}
}
class Student
{
long no;
String name;
float math,eng,comp;
float average;

Student()
{
math=0;
eng=0;
comp=0;
average=0;
}
}class AverageScore{
private final int N=40;
private Student st[]=new Student[N];

void printAverageScore()

{
 FileOutputStream fos =new FileOutputStream("score.txt");


for(int i=0;i<N;i++)
{
st[i]=new Student();
}
for(int j=0;j<N;j++)
{
System.out.print("请输入考生的学号: ");
long temp=InputDate.getLong();
if(temp==0) break;

st[j].no=temp;
System.out.print("请输入学生的名字: ");
st[j].name=InputDate.getString();
System.out.print("请输入该生数学成绩: ");
st[j].math=InputDate.getFloat();
System.out.print("请输入该生物理成绩: ");
st[j].eng=InputDate.getFloat();
        System.out.print("请输入该生化学成绩: ");
st[j].comp=InputDate.getFloat();
}  for(int i=0;i<N;i++)
 {
  st[i].average=(st[i].math+st[i].eng+st[i].comp)/3;
 
 }
 
   
    fos.write(st);
  
  }
 }
 public class Test9_1{
  public static void main(String[] args) throws IOException{
   AverageScore a=new AverageScore();
   a.printAverageScore();
  }
}错误提示:--------------------Configuration: j2sdk1.4.2 <Default>--------------------
D:\java\Test9_1\Test9_1.java:85: cannot resolve symbol
symbol  : method write (Student[])
location: class java.io.FileOutputStream
fos.write(st);
           ^
1 errorProcess completed.请大家一定要帮忙哦,最近真的很忙,学的东西很多,谢谢了!!

解决方案 »

  1.   

    你是关于对象的写入,所以要设计到对象的序列化问题.如果要读出的话就用readObject();可以向下转型到你写入的类型,不清楚的话,去查查ObjectOutputStream和ObjectInputStream.
    修改过的地方已经标出
    import java.io.*;
    class InputDate
    {
    static private String s="";
    static public void input()
    {
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    try{
    s=br.readLine();
    }
    catch(IOException e){}
      }
    static public long getLong()
    {
    input();
    return Long.parseLong(s);
    }
    static public float getFloat()
    {
    input();
    return Float.parseFloat(s);
    }
    static public String getString()
    {
    input();
    return s;
    }
    }
    class Student implements Serializable  )//修改的地方
    {
    long no;
    String name;
    float math,eng,comp;
    float average;

    Student()
    {
    math=0;
    eng=0;
    comp=0;
    average=0;
    }
    }class AverageScore{
    private final int N=40;
    private Student st[]=new Student[N];

    void printAverageScore()

    {
           try{
             FileOutputStream fos=new FileOutputStream ("score.txt");
     ObjectOutputStream oos =new ObjectOutputStream(fos);//修改的地方


    for(int i=0;i<N;i++)
    {
    st[i]=new Student();
    }
    for(int j=0;j<N;j++)
    {
    System.out.print("请输入考生的学号: ");
    long temp=InputDate.getLong();
    if(temp==0) break;

    st[j].no=temp;
    System.out.print("请输入学生的名字: ");
    st[j].name=InputDate.getString();
    System.out.print("请输入该生数学成绩: ");
    st[j].math=InputDate.getFloat();
    System.out.print("请输入该生物理成绩: ");
    st[j].eng=InputDate.getFloat();
            System.out.print("请输入该生化学成绩: ");
    st[j].comp=InputDate.getFloat();
    }  for(int i=0;i<N;i++)
     {
      st[i].average=(st[i].math+st[i].eng+st[i].comp)/3;
     
     }
     
       
       
       for(int i=0;i<N;i++)//修改的地方
       oos.writeObject(st[i]);
        oos.close();
        fos.close();
    }catch(IOException e){}
      }
     }
     public class Test9_1{
      public static void main(String[] args) throws IOException{
       AverageScore a=new AverageScore();
       a.printAverageScore();
      }
    }
      

  2.   

    class Student implements Serializable  )//修改的地方
    上面这个地方多了一个括号)
    注释的时候搞错了.
      

  3.   

    to: kaleon(为人要厚道) 
    是可以运行有结果了,可是输入的文件里是乱码,不知道是怎么回事,有什么方法解决,谢谢了真的很感谢你的帮助,希望你能继续给予我指导,谢谢了
      

  4.   

    给 Student 来一个  toString() 就行了, 然后把那个 write 句改成 循环 写出 每一个 Student.
      

  5.   

    to:humanity(总是偷窥 Java & XML
    怎么来一个啊
    不是很清楚啊,请原谅,可以详细点吗?谢谢!
      

  6.   

    to:kaleon(为人要厚道)那如何写入才不会出现乱码呢?
    还请你能帮我讲解一下
    真的麻烦了
      

  7.   

    那你就用DataOutputStream的下面三个方法之一吧,不过是不能直接写入对象的(String 对象例外).你只能将你要写入的数据一条一条的写入.比如:writeChars(s[i].name),writeChars(s[i].math).
    writeBytes(String s) 
    writeChars(String s)
    writeUTF(String str) 
      

  8.   

    在读入时转换一下编码应该就可以了.
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
      

  9.   

    对象被序列华了,当然是乱码!如果想写文本就用write(String)等方法
      

  10.   

    import java.io.*;
    class InputDate
    {
    static private String s="";
    static public void input()
    { int b;
    //BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    try{
    s=br.readLine();
    }
    catch(IOException e){}
      }
    static public long getLong()
    {
    input();
    return Long.parseLong(s);
    }
    static public float getFloat()
    {
    input();
    return Float.parseFloat(s);
    }
    static public String getString()
    {
    input();
    return s;
    }
    }
    class Student   //修改的地方implements Serializable
    {
    long no;
    String name;
    float math,phy,chim;
    float average;

    Student()
    {
    math=0;
    phy=0;
    chim=0;
    average=0;
    }
    }class AverageScore{
    private final int N=2;
    private Student st[]=new Student[N];

    void printAverageScore()

    {
           try{
             File stu=new File("student.txt");
             RandomAccessFile raf=new RandomAccessFile(stu,"rw");
     //ObjectOutputStream oos =new ObjectOutputStream(fos);//修改的地方


    for(int i=0;i<N;i++)
    {
    st[i]=new Student();
    }
    for(int j=0;j<N;j++)
    {
    System.out.print("请输入考生的学号: ");
    long temp=InputDate.getLong();
    if(temp==0) break;

    st[j].no=temp;
    System.out.print("请输入学生的名字: ");
    st[j].name=InputDate.getString();
    System.out.print("请输入该生数学成绩: ");
    st[j].math=InputDate.getFloat();
    System.out.print("请输入该生物理成绩: ");
    st[j].phy=InputDate.getFloat();
            System.out.print("请输入该生化学成绩: ");
    st[j].chim=InputDate.getFloat();
    }  for(int i=0;i<N;i++)
     {
      st[i].average=(st[i].math+st[i].phy+st[i].chim)/3;
     
     }
     
       
       
       for(int i=0;i<N;i++){//修改的地方
       raf.writeLong(st[i].no);
       raf.writeUTF(st[i].name);
       raf.writeFloat(st[i].math);
       raf.writeFloat(st[i].phy);
       raf.writeFloat(st[i].chim);
       raf.writeFloat(st[i].average);
       
      }
    }catch(IOException e){}
      }
     }
     public class Test9_11{
      public static void main(String[] args) throws IOException{
       AverageScore a=new AverageScore();
       a.printAverageScore();
      }
    }
    我刚修改了下,请大家指正!
      

  11.   

    /*
     * 创建日期 2005-6-3
     *
     * FIXME 要更改此生成的文件的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    package ccccc;import java.io.*;
    class InputDate
    {
    static private String s="";
    static public void input()
    { int b;
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    try{
    s=br.readLine();
    }
    catch(IOException e){}
      }
    static public long getLong()
    {
    input();
    return Long.parseLong(s);
    }
    static public float getFloat()
    {
    input();
    return Float.parseFloat(s);
    }
    static public String getString()
    {
    input();
    return s;
    }
    }
    class Student   //修改的地方implements Serializable
    {
    long no;
    String name;
    float math,phy,chim;
    float average;

    Student()
    {
    math=0;
    phy=0;
    chim=0;
    average=0;
    }
    }class AverageScore{
    private final int N=2;
    private Student st[]=new Student[N];

    void printAverageScore()

    {
           try{
             File stu=new File("student.txt");
             RandomAccessFile raf=new RandomAccessFile(stu,"rw");
     //ObjectOutputStream oos =new ObjectOutputStream(fos);//修改的地方


    for(int i=0;i<N;i++)
    {
    st[i]=new Student();
    }
    for(int j=0;j<N;j++)
    {
    System.out.print("请输入考生的学号: ");
    long temp=InputDate.getLong();
    if(temp==0) break;

    st[j].no=temp;
    System.out.print("请输入学生的名字: ");
    st[j].name=InputDate.getString();
    System.out.print("请输入该生数学成绩: ");
    st[j].math=InputDate.getFloat();
    System.out.print("请输入该生物理成绩: ");
    st[j].phy=InputDate.getFloat();
            System.out.print("请输入该生化学成绩: ");
    st[j].chim=InputDate.getFloat();
    }  for(int i=0;i<N;i++)
     {
      st[i].average=(st[i].math+st[i].phy+st[i].chim)/3;
     
     }
     
       
       
       for(int i=0;i<N;i++){//修改的地方  接楼上的
       raf.writeBytes("" + st[i].no);
           
       raf.writeBytes("" + st[i].name);
       raf.writeBytes("" + st[i].math);
       raf.writeBytes("" + st[i].phy);
       raf.writeBytes("" + st[i].chim);
       raf.writeBytes("" + st[i].average);
       
      }
       
       
    }catch(IOException e){}
      }
     }
     public class Test9_11{
      public static void main(String[] args) throws IOException{
       AverageScore a=new AverageScore();
       a.printAverageScore();
      }