//在Student的构造方法中,定义了一个system.in的流,按照常理,每次创建一个Student对象,其构造方都 会被调用一次,也就是system.in的流会被打开和关闭一次,我只要关闭了就异常,也就是说第二次调用的时候system.in的流没有打开,为什么?
下面是代码:
Student(String name,int age)
{
this.name=name;
this.age=age;
}
Student()
try
{
InputStream is=System.in;
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
System.out.println("请输入学员姓名");
this.name=br.readLine();
System.out.println(is.available());
System.out.println("请输入该学员语文成绩");
this.Chinese = Integer.parseInt(br.readLine());
System.out.println("请输入该学员数学成绩");
this.Math = Integer.parseInt(br.readLine());
System.out.println("请输入该学员英语成绩");
this.English = Integer.parseInt(br.readLine());
//br.close(); 此处不能关闭,关闭就报异常,求解
}
catch(IOException e){
System.out.println(e.toString());
}
}
}
public class ScoreSortTest {
public static void main(String[] args) //throws IOException
{
Student s1=new Student();
Student s2=new Student();
TreeSet<Student> ts=new TreeSet<Student>(new StudentComparator());
ts.add(s1);
ts.add(s2);
for(Student s:ts)
System.out.println("姓名:"+s.name+"总成绩:"+(s.Chinese+s.English+s.Math));
}
} |
|