public class Student {
private String name;
private int chinese;
private int math;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getChinese() {
return chinese;
}
public void setMath(int math) {
this.math = math;
}
public int getMath() {
return math;
}
public Student() {
}
public Student(String name, int chinese, int math) {
this.name = name;
this.chinese = chinese;
this.math = math;
}
public void show() {
System.out.println(name + "的语文成绩:" + chinese + ",数学成绩:" + math + ",总成绩:" + (chinese + math));
}
}
测试类
package com.Test.Demo01;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student stu1 = new Student("邓超",88,99);
Student stu2 = new Student();
stu2.setName("baby");
stu2.setChinese(85);
stu2.setMath(78);
Student stu3 = new Student();
stu3.setName("郑凯");
stu3.setChinese(86);
stu3.setMath(50);
list.add(stu1);
list.add(stu2);
list.add(stu3);
Student stud = new Student();
ArrayList<Student> list1 = new ArrayList<>();
list1 = getSum(list);
for (int i = 0; i < list1.size(); i++) {
String name = list1.get(i).getName();
int chinese = list1.get(i).getChinese();
int math = list1.get(i).getMath();
stud.setName(name);
stud.setChinese(chinese);
stud.setMath(math);
stud.show();
}
}
public static ArrayList<Student> getSum(ArrayList<Student> list){
int sum = 0;
ArrayList<Student> list1 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
int chinese = list.get(i).getChinese();
int math = list.get(i).getMath();
sum = chinese + math;
if (sum > 160){
list1.add(list.get(i));
}
}
return list1;
}
}