日期:2014-05-20  浏览次数:20638 次

求助,请高手给看下程序指点一下
School类:
import java.util.ArrayList;

public class School {
Student s = new Student();
ArrayList<Student> list = new ArrayList<Student>();

//添加学生
public void add(int id,String name,int age,boolean sex,int score){
s.setId(id);
s.setName(name);
s.setAge(age);
s.setSex(sex);
s.setScore(score);
list.add(s);
}

//删除学生
public void del(int id){
list.remove(id);
}

//按学号查询学生
public void select(int id){
boolean x = false;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getId()==id){
System.out.println("学号:"+list.get(i).getId()+"\t姓名:"+list.get(i).getName()+"\t年龄:"+list.get(i).getAge()+"\t性别:"+list.get(i).isSex()+"\t成绩:"+list.get(i).getScore());
x = true;
break;
}
}
if(x==false){
System.out.println("系统没有找到记录!");
}
}

//打印所有学生信息列表
public void print(){
for (int i = 0; i < list.size(); i++) {
System.out.println("学号:"+list.get(i).getId()+"\t姓名:"+list.get(i).getName()+"\t年龄:"+list.get(i).getAge()+"\t性别:"+list.get(i).isSex()+"\t成绩:"+list.get(i).getScore());
}
}

}



Student类:
public class Student {
private int id;
private String name;
private int age;
private boolean sex;
private int score;
public Student() {
}
public Student(int id,String name,int age,boolean sex,int score) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}


Test类:
public class Test {

public static void main(String[] args) {
School s = new School();
s.add(01, "zhangliang", 18, true, 92);
s.add(02, "maojinlong", 19, true, 78);
s.add(03, "baisha", 16, false, 86);
s.print();
System.out.println("=================");
s.del(2);
s.print();
System.out.println("========查询========");
s.select(01);
}

}


我这个程序为什么添加上总出问题,请高手指点一下!

------解决方案--------------------
把 Student s = new Student();
放在 add方法里边。