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"));
麻烦大家了!
你先把IOException改成Exception试试。