有梦花无缺 2023-03-30 17:02 采纳率: 100%
浏览 10
已结题

java练习题实际结果与预估计结果差异(开发工具-intellij-idea)

求解答疑惑
一下是自己编写的一道java练习题的代码
所得实际结果与预输出结果有差异

预输出结果为: ArrayList集合中的学生对象按成绩排序,降序排序输出,如果成绩一样就按年龄升序排序

[Student{name='liuSan', age=20, score=90.0}, Student{name='liSi', age=22, score=90.0}, Student{name='wanGwu', age=20, score=99.0}, Student{name='sunLiu', age=22, score=100.0}]
Student{name='sunLiu', age=22, score=100.0}
Student{name='wanGwu',age=20,score=99.0}
Student{name='liuSan', age=20, score=90.0}
Student{name='liSi', age=22, score=90.0}

实际输出结果wei:

[Student{name='liuSan', age=20, score=90.0}, Student{name='liSi', age=22, score=90.0}, Student{name='wanGwu', age=20, score=99.0}, Student{name='sunLiu', age=22, score=100.0}]
Student{name='sunLiu', age=22, score=100.0}
Student{name='liuSan', age=20, score=90.0}
Student{name='liSi', age=22, score=90.0}

以下为具体代码:


```java
import java.util.*;

public class Homework03  {


    public static void main(String[] args) {
        Student s1 = new Student("liuSan",20,90.0F);
        Student s2 = new Student("liSi",22,90.0F);
        Student s3 = new Student("wanGwu",20,99.0F);
        Student s4 = new Student("sunLiu",22,100.0F);
        ArrayList<Student> c = new ArrayList<>();
        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);
        System.out.println(c);

        TreeSet<Student> c1 = new TreeSet<>(new AComparator());

        c1.addAll(c);

        for (Student stu : c1) {
            System.out.println(stu);
        }





    }

}



class AComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        if(o1.score - o2.score > 0 || o1.score - o2.score < 0) {
            return o2.age - o1.age;
        }
        return o1.age - o2.age;


    }
}

class Student implements Comparable<Student>{
    String name ;
    int age ;
    float score ;

    public Student() {
    }

    public Student(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    @Override
    public int compareTo(Student s) {
        if(this.score > s.getScore()) return 1;
        else if(this.score < s.getScore()) return -1;
        else if(this.score == s.getScore()) {
            if (this.age > s.getAge()) return -1;
            else if (this.age == s.getAge()) return  0;
            return 1;
            }
        return 1;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" +name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

```

  • 写回答

2条回答 默认 最新

  • 瞬间的未来式 2023-03-30 17:23
    关注

    试试这个:

    class AComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.score == o2.score) {
                return o1.age - o2.age;
            } else {
                return Float.compare(o2.score, o1.score);
            }
        }
    }
    
    
    

    这里我们先比较成绩,如果成绩相等,再比较年龄,实现了您的预期结果。注意在比较成绩时使用 Float.compare() 方法比较浮点数,而不是使用减法来比较。这是因为在 Java 中使用减法比较浮点数时可能会存在精度误差,从而导致比较结果不正确。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测