创建学生类:
public class Student {
public int sno;
public String name;
public int ScoreEnglish;
public int ScorePhysical;
public int ScoreHmath;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int sno, String name, int ScoreEnglish, int ScorePhysical, int ScoreHmath) {
this.sno = sno;
this.name = name;
this.ScoreEnglish = ScoreEnglish;
this.ScorePhysical = ScorePhysical;
this.ScoreHmath = ScoreHmath;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScoreEnglish() {
return ScoreEnglish;
}
public void setScoreEnglish(int ScoreEnglish) {
this.ScoreEnglish = ScoreEnglish;
}
public int getScorePhysical() {
return ScorePhysical;
}
public void setScorePhysical(int ScorePhysical) {
this.ScorePhysical = ScorePhysical;
}
public int getScoreHmath() {
return ScoreHmath;
}
public void setScoreHmath(int ScoreHmath) {
this.ScoreHmath = ScoreHmath;
}
}
实现学生类中查询的方法
import java.util.ArrayList;
public class StudentList {
ArrayList<Student> student = new ArrayList<>();// 对象数组
// 实现匹配查找 例如输入“张”就能查询到名字带有“张”的学生
public String Query(String name) {
for (int i = 0; i < student.size(); i++) {
if (student.get(i).getName().contains(name)) {
System.out.println("学号:" + student.get(i).getSno() + " " + "姓名:" + student.get(i).getName() + " "
+ "英语成绩:" + student.get(i).getScoreEnglish() + " " + "体育成绩:" + student.get(i).getScorePhysical()
+ " " + "高数成绩:" + student.get(i).getScoreHmath());
} else {
return null;
}
}
return name;
}
// 查询所有学生的信息
public void print() {
for (int i = 0; i < student.size(); i++) {
System.out.println("学号:" + student.get(i).getSno() + " " + "姓名:" + student.get(i).getName() + " "
+ "英语成绩:" + student.get(i).getScoreEnglish() + " " + "体育成绩:" + student.get(i).getScorePhysical()
+ " " + "高数成绩:" + student.get(i).getScoreHmath());
}
}
// 增加学生的方法
public void studentadd(Student stu) {
student.add(stu);
}
// 查询英语成绩 如果没有60分就打印不及格
public void QueryEnglish() {
int people = 0;
System.out.print("英语不及格的有:");
for (int i = 0; i < student.size(); i++) {
if (student.get(i).getScoreEnglish() < 60) {
people++;
System.out.print(" " + student.get(i).getName());
}
}
System.out.println(" " + "总共有:" + people + "人");
}
// 查询体育成绩 如果没有60分就打印不及格
public void QueryPhysical() {
int people = 0;
System.out.print("体育不及格的有:");
for (int i = 0; i < student.size(); i++) {
if (student.get(i).getScorePhysical() < 60) {
people++;
System.out.print(" " + student.get(i).getName());
}
}
System.out.println(" " + "总共有:" + people + "人");
}
// 查询高数成绩 如果没有60分就打印不及格
public void QueryHmath() {
int people = 0;
System.out.print("高数不及格的有:");
for (int i = 0; i < student.size(); i++) {
if (student.get(i).getScoreHmath() < 60) {
people++;
System.out.print(" " + student.get(i).getName());
}
}
System.out.println(" " + "总共有:" + people + "人");
}
}
测试学生类
public class StudnetTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//实例化student对象
Student student1 = new Student(3569, "八重樱", 95, 83, 59);
Student student2 = new Student(4523, "卡莲", 55, 77, 85);
Student student3 = new Student(5721, "琪亚娜", 88, 59, 76);
Student student4 = new Student(7777, "符华", 62, 77, 87);
Student student5 = new Student(4396, "布诺妮娅", 73, 79, 87);
Student student6 = new Student(4981, "姬子", 51, 84, 87);
Student student7 = new Student(6521, "德利莎", 58, 54, 87);
StudentList studentList = new StudentList();
//调用StudentList里面的studentadd方法
studentList.studentadd(student1);
studentList.studentadd(student2);
studentList.studentadd(student3);
studentList.studentadd(student4);
studentList.studentadd(student5);
studentList.studentadd(student6);
studentList.studentadd(student7);
studentList.Query("重");
//调用查询英语 体育 高数成绩以及所有学生信息的方法
studentList.QueryEnglish();
studentList.QueryPhysical();
studentList.QueryHmath();
//studentList.print();
}
}