定义一个学生类用来存储学生的信息(包括:学号,姓名,英语成绩、高数成绩、体育成绩),在main方法中定义一个数组存储5名学生的信息。
要求:
(1)按名字查询并输出某位同学成绩,要求能够实现部分匹配的查找,例如:希望查找John, 可查找到所有名字包含John 的人,例如: John Brown, John Smith都能得到(可以使用字符串类String的contains方法,来判断某个字符串中是否包含某个子串)。
(2)查询并输出所有科目不及格的人数及名单。
输出格式为:
英语成绩不及格的有:张三、李四、王五,总共有3人
高数成绩不及格的有:张三,总共有1人
体育成绩不及格的有:张三丰、张无忌,总共有2人
提示:
(1)定义的学生类:包括属性、属性的getter、setter 方法。
(2)类中其他方法可自由发挥,比如打印学生信息的方法(如print(或toString())
(3)自定义包含main方法的类,类中定义2个方法,分别用来实现按名字查询成绩、查找不及格人数并打印不及格名单的功能。
Student.java
public class Student {//定义一个学生类(学号,姓名,英语、高数、体育成绩)
//属性
private String number;
private String name;
private int englishScore;
private int mathScore;
private int sportsScore;
//方法
public String getNumber() {//学号
return number;
}
public void setNumber() {
this.number = number;
}
public String getName() {//姓名
return name;
}
public void setName() {
this.name = name;
}
public int getEnglishScore() {//英语
return englishScore;
}
public void setEnglishScore() {
this.englishScore = englishScore;
}
public int getMathScore() {//高数
return mathScore;
}
public void setMathScore() {
this.mathScore = mathScore;
}
public int getSportsScore() {//体育
return sportsScore;
}
public void setSportsScore() {
this.sportsScore = sportsScore;
}
public Student(String number, String name, int englishScore, int mathScore, int sportsScore) {
super();
this.number = number;
this.name = name;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.sportsScore = sportsScore;
}
public String toString() {
return "Student[ number = "+number+", name = "+name+", englishScore = "+englishScore+", mathScore = "+mathScore+", sportsScore = "+sportsScore+" ]";
}
}
TextStudent.java
public class TextStudent {
private static Student[] stu;//声明对象数组
/**
* @param args
*/
public static void main(String[] args) {
stu = new Student[5];//实例化对象数组
stu[0] = new Student("1","张三",57,45,91);
stu[1] = new Student("2","李四",58,82,92);
stu[2] = new Student("3","王五",59,83,93);
stu[3] = new Student("4","张三丰",74,84,44);
stu[4] = new Student("5","张无忌",75,85,55);
search("a");
serchfail();
}
/**
* 查成绩不及格的人数
*/
private static void serchfail() {
// TODO Auto-generated method stub
System.out.print("英语成绩不及格的有:");
int a = 0;
for (int i = 0; i < stu.length; i++) {
if (stu[i].getEnglishScore()<60) {
System.out.print(stu[i].getName()+"、");
a++;
}
}
System.out.println();
System.out.print("总共有:"+a);
System.out.println();
System.out.print("高数成绩不及格的有:");
int b = 0;
for (int i = 0; i < stu.length; i++) {
if (stu[i].getMathScore()<60) {
System.out.print(stu[i].getName()+"、");
b++;
}
}
System.out.println();
System.out.print("总共有:"+b);
System.out.println();
System.out.print("体育成绩不及格的有:");
int c = 0;
for (int i = 0; i < stu.length; i++) {
if (stu[i].getSportsScore()<60) {
System.out.print(stu[i].getName()+"、");
c++;
}
}
System.out.println();
System.out.print("总共有:"+c);
System.out.println();
}
/**
* 根据姓名查找成绩
*/
private static void search(String name) {
// TODO Auto-generated method stub
for (int i = 0; i < stu.length; i++) {
if (stu[i].getName().contains(name)) {
System.out.println(stu[i]);
}
}
}
}
打印: