用java语言修改年龄的大小_Java语言编写Student(姓名:年龄:成绩)信息排序,重写compareTo()方法...

public class Student implements Comparable{

private String name;

private int age;

private int score;

public Student() {

super();

}

public Student(String name,int age,int score) {

super();

setName(name);

setAge(age);

setScore(score);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

if(age > 0 && age < 50){

this.age = age;

}else{

System.out.println("年龄不合理");

}

}

public int getScore() {

return score;

}

public void setScore(int score) {

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Java编写一个学生成绩排序的程序,该程序包括学生的姓名、学号和三门课程的成绩,并按照总分进行排序: ```java import java.util.*; class Student implements Comparable<Student> { private String name; private String id; private double score1; private double score2; private double score3; private double totalScore; public Student(String name, String id, double score1, double score2, double score3) { this.name = name; this.id = id; this.score1 = score1; this.score2 = score2; this.score3 = score3; this.totalScore = score1 + score2 + score3; } public String getName() { return name; } public String getId() { return id; } public double getScore1() { return score1; } public double getScore2() { return score2; } public double getScore3() { return score3; } public double getTotalScore() { return totalScore; } @Override public int compareTo(Student other) { if (this.totalScore > other.totalScore) { return -1; } else if (this.totalScore < other.totalScore) { return 1; } else { return 0; } } } public class SortStudents { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student("Alice", "001", 80.0, 90.0, 85.0)); students.add(new Student("Bob", "002", 75.0, 85.0, 90.0)); students.add(new Student("Charlie", "003", 90.0, 80.0, 75.0)); Collections.sort(students); for (Student student : students) { System.out.println(student.getName() + " " + student.getId() + " " + student.getTotalScore()); } } } ``` 以上程序中,`Student`类包括学生的基本信息以及三门课的成绩,还包括计算总分的方法。为了能够对学生按照总分进行排序,`Student`类实现了`Comparable`接口,并重写了`compareTo`方法。`SortStudents`类创建了一个包含若干个`Student`对象的List,并使用`Collections.sort`方法对其进行排序。最后,程序输出排序后的学生信息,包括姓名、学号和总分。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值