firebase 如何从学生成绩和指数中找到中位数并将其设置为排名

aiazj4mn  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(83)

嗨,我有这套学生成绩降序排列与各自的索引/序号(自动生成的班级规模)。
我想在学生队伍中做到这一点
| 索引(序列号)|评分|秩|
| --|--|--|
| 1. |九十五点零|2.0|
| 2. |九十五点零|2.0|
| 3. |九十五点零|2.0|
| 4. |九十点|4.5|
| 5. |九十点|4.5|
| 6. |八十五点零|6.0版本|
| 7. |70.0|第九章|
| 8. |70.0|第九章|
| 9. |70.0|第九章|
| 10. |70.0|第九章|
| 11. |70.0|第九章|
| 12. |六十五点零|十二点五|
| 13. |六十五点零|十二点五|
| 14. |六十五点零|十二点五|
| 15. |六十五点零|十二点五|
| 16. |60.0|十六点零|
| 17. |40.0|十七点零|
对于指数为1,2和3的学生,他们的排名为2,这意味着班上没有第一名。排名(对于我们的情况是2)通过找到这三个数字的中间值(中位数)来找到。
索引6仍然是类中的排名6,这是可以的。类中没有排名5,这也是可以的。
指数4和5的总数为2,是偶数,并且得分相等,因此我们发现中位数指数为(4+5)2= 4.5
索引7、8、9、10和11的总数为5,是奇数,得分相等,因此通过查找中位数9来查找排名。(7+8+9+10+11)/5=9
因此,我想实现的是计算中位数和给予相同的排名的学生的分数是平等的,他们的计数是偶数。例如下面的代码结果应该是8.5,而不是8.0,因为这些指标的中位数是(6.0+7.0+8.0+9.0+10.0+11.0)/6=8.5,对于指数12.0和13.0,中位数应该是12.5而不是12.0,因为(12.0+13.0)/2= 12.5。注意:对于指数1.0至5.0的情况,它工作正常。
下面是我的代码,给予以下结果:

public class ViewStudentScores extends AppCompatActivity {

private DatabaseReference databaseReference;
private StudentAdapter studentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_student_scores);

    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    studentAdapter = new StudentAdapter();
    recyclerView.setAdapter(studentAdapter);
    databaseReference = FirebaseDatabase.getInstance().getReference("students");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Student> students = new ArrayList<>();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Student student = snapshot.getValue(Student.class);
                students.add(student);
            }

            // Sort students by score in descending order
            Collections.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student student1, Student student2) {
                    return Double.compare(student2.getScore(), student1.getScore());
                }
            });

            // Assign ranks considering median of index numbers for students with the same score
            int totalStudents = students.size();
            int startIndex = 0;
            while (startIndex < totalStudents) {
                int endIndex = startIndex + 1;
                while (endIndex < totalStudents && students.get(startIndex).getScore() == students.get(endIndex).getScore()) {
                    endIndex++;
                }
                double medianIndex = (startIndex + endIndex - 1) / 2;
                double rank = medianIndex + 1;

                // Assign rank to students with the same score
                for (int i = startIndex; i < endIndex; i++) {
                    students.get(i).setRank(rank);
                }

                startIndex = endIndex;
            }

            // Update RecyclerView with the sorted and ranked students
            studentAdapter.setStudents(students);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            // Handle database error
        }
    });
}

字符串
}
x1c 0d1x的数据

h7appiyu

h7appiyu1#

在你计算中位数的那条线上

double medianIndex = (startIndex + endIndex - 1) / 2;

字符串
你正在做整数除法,因为两边都是整数,因为startIndexendIndex是整数。要么将它们中的一个或两个都转换为double,要么将除数2设置为double。最简单的方法可以是:

double medianIndex = (startIndex + endIndex - 1) / 2.0;

相关问题