A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蔡徐坤 初级黑马   /  2019-3-22 15:06  /  901 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.Test.Demo01;

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;
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马