java编程

SDDDMW 2020-04-09 11:44:52
已知学生类s6_Stud4,成员变量有no(学号,int)、name(姓名,String)、chinese(语文成绩,int)和math(数学成绩,int)。编程,要求按总成绩降序排列,若总成绩相同,按语文成绩降序排列,并编制简单的测试类。
...全文
107 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
paullbm 2020-04-09
  • 打赏
  • 举报
回复
楼主,及时结贴吧!

package csdn;

import java.util.ArrayList;

public class Student {
	private int no;
	private String name;
	private int chinese;
	private int math;

	public Student(int no, String name, int chinese, int math) {
		this.no = no;
		this.name = name;
		this.chinese = chinese;
		this.math = math;
	}

	public int getNo() {
		return no;
	}

	public String getName() {
		return name;
	}

	public int getChinese() {
		return chinese;
	}

	public int getMath() {
		return math;
	}

	public int getScores() {
		return this.chinese + this.math;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		sb.append("no=").append(no);
		sb.append(",name=").append(name);
		sb.append(",chinese=").append(chinese);
		sb.append(",math=").append(math);
		sb.append(",scores=").append(this.getScores());
		sb.append("]");
		return sb.toString();
	}

	public static void sortProcess(ArrayList<Student> stuList) {
		int size = stuList.size();
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				if ((stuList.get(j).getScores() < stuList.get(i).getScores())
						|| ((stuList.get(j).getScores() == stuList.get(i).getScores())
								&& (stuList.get(j).getChinese() < stuList.get(i).getChinese()))) {
					Student temp = stuList.get(j);
					stuList.set(j, stuList.get(i));
					stuList.set(i, temp);
				}
			}
		}
		for (Student stu : stuList) {
			System.out.println(stu);
		}
	}

	public static void main(String[] args) {
		ArrayList<Student> stuList = new ArrayList<Student>();
		Student stu1 = new Student(1, "张三", 80, 85);
		Student stu2 = new Student(2, "李四", 95, 83);
		Student stu3 = new Student(3, "王五", 85, 80);
		Student stu4 = new Student(4, "赵六", 82, 83);
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);
		stuList.add(stu4);
		sortProcess(stuList);
	}
}
  • 打赏
  • 举报
回复
package com.example.demo.vo;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Student implements Serializable {
    private  int no;
    private String name;
    private int  chinese;
    private  int math;
    private  int sum;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getSum() {
        return sum;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", sum=" + sum +
                '}';
    }
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        Student student1 = new Student();
        student1.setNo(1);
        student1.setName("name1");
        student1.setChinese(90);
        student1.setMath(92);
        student1.setSum(student1.getChinese()+student1.getMath());
        list.add(student1);

        Student student2 = new Student();
        student2.setNo(2);
        student2.setName("name2");
        student2.setChinese(90);
        student2.setMath(94);
        student2.setSum(student2.getChinese()+student2.getMath());
        list.add(student2);

        Student student3 = new Student();
        student3.setNo(3);
        student3.setName("name3");
        student3.setChinese(95);
        student3.setMath(89);
        student3.setSum(student3.getChinese()+student3.getMath());
        list.add(student3);

        Student student4 = new Student();
        student4.setNo(4);
        student4.setName("name4");
        student4.setChinese(96);
        student4.setMath(92);
        student4.setSum(student4.getChinese()+student4.getMath());
        list.add(student4);

        List<Student> list2 = list
                .stream()
                .sorted(Comparator.comparing(Student::getSum).thenComparing(Student::getChinese).reversed())
                .collect(Collectors.toList());
        System.out.print(list2);

    }


}

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧