面相对象往一个数组里添加学生,显示学生信息

文件1:

public class Stu {
String name;
int age;
int score;
}

文件2:
 public class T2 {
Stu[] stus = new Stu[30];
String[] name = new String[30];

void addstu(Stu stu) {
for (int i = 0; i < stus.length; i++) {
if (stus[i] == null) {
stus[i] = stu;
break;
}
}

}

void show() {
for (int i = 0; i < stus.length; i++) {
if (stus[i]!= null) {
System.out.println(stus[i].name+"\t"+stus[i].age+"\t"+stus[i].score);
}else{
break;
}
}
}

public static void main(String[] args) {
T2 t2 = new T2();
Stu stu1 = new Stu();
stu1.name = "大王";
stu1.age = 20;
stu1.score = 60;
Stu stu2 = new Stu();
stu2.name = "小王";
stu2.age = 18;
stu2.score = 80;
t2.addstu(stu1);
t2.addstu(stu2);
t2.show();
}
}
posted @ 2017-07-21 11:01  斯丢皮德曼  阅读(365)  评论(0编辑  收藏  举报