import java.io.*;
class InputData
{
static private String s="";
static public void input()
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
try{
s=br.readLine();
}
catch(IOException e){}
}
static public long getLong()
{
input();
return Long.parseLong(s);
}
static public int getInt()
{
input();
return Integer.parseInt(s);
}
static public String getString()
{
input();
return s;
}
}
class Student {
long no;
String name;
int math,phy,chim;
int average;
Student()
{
math=0;
phy=0;
chim=0;
average=0;
}
}class StudentScore{
private final int N=1;
private Student st[]=new Student[N];
void printStudent()
{
try{
File stu=new File("student.dat");
RandomAccessFile raf=new RandomAccessFile(stu,"rw");
for(int i=0;i<N;i++)
{
st[i]=new Student();
}
for(int j=0;j<N;j++)
{
System.out.print("请输入第"+(j+1)+"位学生的学号: ");
long temp=InputData.getLong();
if(temp==0) break;
st[j].no=temp;
System.out.print("请输入第"+(j+1)+"位学生的名字: ");
st[j].name=InputData.getString();
System.out.print("请输入第"+(j+1)+"位学生数学成绩: ");
st[j].math=InputData.getInt();
System.out.print("请输入第"+(j+1)+"位学生物理成绩: ");
st[j].phy=InputData.getInt();
System.out.print("请输入第"+(j+1)+"位学生化学成绩: ");
st[j].chim=InputData.getInt();
} 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(" physics=" + st[i].phy);
raf.writeBytes(" chemistry=" + st[i].chim);
raf.writeBytes(" averageScore=" + st[i].average+"\n");
}
System.out.print("学号\t"+"姓名\t"+"数学\t"+"物理\t");
System.out.println("化学\t"+"平均成绩");
for(int j=0;j<N;j++)
{
System.out.print(st[j].no+"\t"+st[j].name+"\t"+st[j].math+"\t");
System.out.println(st[j].phy+"\t"+st[j].chim+"\t"+st[j].average);
}
}catch(IOException e){}
}
}
public class Test9_11{
public static void main(String[] args) throws IOException{
StudentScore stu=new StudentScore();
stu.printStudent();
}
}在输入的文件里不能显示中文,就是乱码了,使用BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
又出现下面错误,请大家指点一下,谢谢!unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
麻烦大家了!
class InputData
{
static private String s="";
static public void input()
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
try{
s=br.readLine();
}
catch(IOException e){}
}
static public long getLong()
{
input();
return Long.parseLong(s);
}
static public int getInt()
{
input();
return Integer.parseInt(s);
}
static public String getString()
{
input();
return s;
}
}
class Student {
long no;
String name;
int math,phy,chim;
int average;
Student()
{
math=0;
phy=0;
chim=0;
average=0;
}
}class StudentScore{
private final int N=1;
private Student st[]=new Student[N];
void printStudent()
{
try{
File stu=new File("student.dat");
RandomAccessFile raf=new RandomAccessFile(stu,"rw");
for(int i=0;i<N;i++)
{
st[i]=new Student();
}
for(int j=0;j<N;j++)
{
System.out.print("请输入第"+(j+1)+"位学生的学号: ");
long temp=InputData.getLong();
if(temp==0) break;
st[j].no=temp;
System.out.print("请输入第"+(j+1)+"位学生的名字: ");
st[j].name=InputData.getString();
System.out.print("请输入第"+(j+1)+"位学生数学成绩: ");
st[j].math=InputData.getInt();
System.out.print("请输入第"+(j+1)+"位学生物理成绩: ");
st[j].phy=InputData.getInt();
System.out.print("请输入第"+(j+1)+"位学生化学成绩: ");
st[j].chim=InputData.getInt();
} 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(" physics=" + st[i].phy);
raf.writeBytes(" chemistry=" + st[i].chim);
raf.writeBytes(" averageScore=" + st[i].average+"\n");
}
System.out.print("学号\t"+"姓名\t"+"数学\t"+"物理\t");
System.out.println("化学\t"+"平均成绩");
for(int j=0;j<N;j++)
{
System.out.print(st[j].no+"\t"+st[j].name+"\t"+st[j].math+"\t");
System.out.println(st[j].phy+"\t"+st[j].chim+"\t"+st[j].average);
}
}catch(IOException e){}
}
}
public class Test9_11{
public static void main(String[] args) throws IOException{
StudentScore stu=new StudentScore();
stu.printStudent();
}
}在输入的文件里不能显示中文,就是乱码了,使用BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
又出现下面错误,请大家指点一下,谢谢!unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
麻烦大家了!
应该是GB2312不支持,需要编码转换,支持的编码java_doc可以找到
这个有关系吗
BufferedReader br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
s=br.readLine();
}
catch(IOException e){}
应该这样写,搞定了。
raf写进去还有问题,
好象InputStreamReader不支持
Charset
Description
US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit UCS Transformation Format
UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order
* 创建日期 2005-6-3
*
* FIXME 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package ccccc;import java.io.*;
class InputDate
{
static private String s="";
static public void input()
{ int b;
BufferedReader br;
try{
br =new BufferedReader(new InputStreamReader(System.in,"gb2312"));
s=br.readLine();
}
catch(Exception 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");
// long fileLen = raf.length();
// System.out.print("fileLen :" + fileLen );
// raf.seek(fileLen);
// fw.write("tangwenhua");
// fw.write("兄弟 你好啊");
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;
}
FileWriter raf = new FileWriter("student.txt",true);
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 + "\n");*/
raf.write(" 学号: " + st[i].no);
raf.write(" 姓名: " + st[i].name);
raf.write(" 数学: " + st[i].math);
raf.write(" 物理: " + st[i].phy);
raf.write(" 化学: " + st[i].chim);
raf.write(" 平均: " + st[i].average + "\n");
}
raf.flush();
raf.close();
}catch(IOException e){}
}
}
public class Test9_11{
public static void main(String[] args) throws IOException{
AverageScore a=new AverageScore();
a.printAverageScore();
}
}
特别是xiangqianchong(向前冲)兄弟,感谢他的无私帮助
谢谢了!