想编写一个用来输入全班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.请大家一定要帮忙哦,最近真的很忙,学的东西很多,谢谢了!!
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.请大家一定要帮忙哦,最近真的很忙,学的东西很多,谢谢了!!
修改过的地方已经标出
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();
}
}
上面这个地方多了一个括号)
注释的时候搞错了.
是可以运行有结果了,可是输入的文件里是乱码,不知道是怎么回事,有什么方法解决,谢谢了真的很感谢你的帮助,希望你能继续给予我指导,谢谢了
怎么来一个啊
不是很清楚啊,请原谅,可以详细点吗?谢谢!
还请你能帮我讲解一下
真的麻烦了
writeBytes(String s)
writeChars(String s)
writeUTF(String str)
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
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();
}
}
我刚修改了下,请大家指正!
* 创建日期 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();
}
}