根据分数JAVA比较学生 [英] Comparing students according to score JAVA

查看:173
本文介绍了根据分数JAVA比较学生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一班学生。该类具有字符串名称 int studentId int studentScore 。我想比较 ArrayList< Student>中的学生。学生根据学生的说法。这意味着如果学生1和学生2具有相同数量的学生核心,那么他们的两个等级将是相同的。我有一个功能,我设置排名。但我不确定如何比较arraylist中的所有内容。

I have a class of Student. The class has String name, int studentId and int studentScore. I want to compare the students in ArrayList<Student> students according to the studentscore. This means if "student 1" and "student 2" has the same amount of studentscore then both of their ranks will be the same. I have a function where I set The rank. But I'm unsure how to compare everything in the arraylist.

当我搜索比较int值时,我得到结果:

When I search for comparing int values I get the results:

如何在Java中正确比较两个整数?

我认为最好的解决方案是使用比较器类功能。但我不确定如何做到这一点因为我不确定比较器类的能力。我想到了循环获得一个玩家的循环,然后再循环和比较。但这似乎不是一个好的解决方案。

I think the best solution will be to use comparator class function. But Im unsure how to do this because I'm unsure of the comparator classes capability. I was think of for looping getting a player by that loop then for looping again and comparing. But this doesnt seem like a good solution.


我想要这个结果:

I want this result:

假设学生数组包含(姓名,学生核心):

Say that the array of students contains (name,studentscore) is:

(kim, 12), (werder, 20), (ben, 32), (sara, 12)

I想要学生的等级(如果ASC订单):

I want the rank of the student to be (If ASC order):

kim: rank = 1
werder: 2
ben: 3
sara: 1


推荐答案

您应该使用java Stream(Java 8)。
更好的方法来做到这一点。
您可以找到一个非常好的例子: http://www.leveluplunch.com/java/tutorials/007-sort-arraylist-stream-of-objects-in-java8/

You should use java Stream (Java 8). Better way to do this. You can found a really good exemple at : http://www.leveluplunch.com/java/tutorials/007-sort-arraylist-stream-of-objects-in-java8/

这篇关于根据分数JAVA比较学生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆