汇才同飞教育汇才同飞学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › 王恒 › 日志

创建一个Student类的数组,封装5个学生的信息,按指定条件查找学生

2019-02-16 13:58:06 查看(416) 回复(0)

代码要求:

1、创建一个学生类,包含三个属性:姓名、年龄、成绩

2、创建一个长度为5的学生类的数组,分别创建5个学生对象,装入数组中:

姓名 年龄 成绩
刘海东 21 87
王梦月 22 85
韩少卿 20 89
李岩华 23 84
王正文 21 85

3、查找1:在数组中查找姓名是“王梦月”的学生信息

4、查找2:在数组中查找年龄是22,成绩是85的学生信息

package day1;


public class StudentTest {
public static void main(String[] args) {
Students a = new Students();
a.name = "刘海东";
a.age = 21;
a.score = 87;
Students b = new Students();
b.name = "王梦月";
b.age = 22;
b.score = 85;
Students c = new Students();
c.name = "韩少卿";
c.age = 20;
c.score = 89;
Students d = new Students();
d.name = "李岩华";
d.age = 23;
d.score = 84;
Students e = new Students();
e.name = "王正文";
e.age = 21;
e.score = 85;
int index=0;
Students[] students = { a, b, c, d, e, };
for (int i = 0; i < students.length; i++) {
if (students[i].name == "王梦月") {
index = i;
}

}System.out.println(students[index]);

for(int i=0;i<students.length;i++) {
if(students[i].age==22 && students[i].score==85) {
index=i;
}
}
System.out.println(students[index]);

}

}


package day1;


public class Students {
@Override
public String toString() {
return "Students [name=" + name + ", age=" + age + ", score=" + score + "]";
}
String name;
int age;
int score;
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}


}


评论 (0 个评论)

facelist

全部作者的其他最新日志

站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

返回顶部