
正文
JAVA成绩排序名字代码 java成绩排序名字代码怎么输入
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java语言做成绩排名表,如何实现同分同名次,最好有代码,谢谢
思路: 排序肯定还是要排的, 按照Java成绩来进行排练. 然后排名的时候,进行比较. 如果这一名的成绩和上一名的相同, 那么名次相同, 如果比上一名分数低,那么排名加一.
可以使用传统的,集合排序,输出. 也可以使用java8新提供的Stream API进行操作
参考代码如下
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
class Stu {// 学生类
private String name;
private double score;// 成绩
public Stu(String name, double score) {
this.name = name;
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//测试类
public class TestDemo {
public static void main(String[] args) {
ListStu stus = Arrays.asList(new Stu("Tom", 79.5), new Stu("Jack", 52), new Stu("Amdy", 79.5),
new Stu("Lucy", 68), new Stu("Cherry", 79.5), new Stu("Jerry", 52), new Stu("Sweet", 91),
new Stu("Solem", 65));
fun1(stus);
System.out.println("---------------分割线---------------------");
fun2(stus);
}
// 方法一:传统的方法
public static void fun1(ListStu stus) {
// 按照成绩排序
stus.sort(new ComparatorStu() {
@Override
public int compare(Stu s1, Stu s2) {
return -Double.compare(s1.getScore(), s2.getScore());
}
});
int index = 0;// 排名
double lastScore = -1;// 最近一次的分
for (int i = 0; i stus.size(); i++) {
Stu s = stus.get(i);
if (Double.compare(lastScore, s.getScore())!=0) { // 如果成绩和上一名的成绩不相同,那么排名+1
lastScore = s.getScore();
index++;
}
System.out.println("名次:" + index + "\t分数" + s.getScore() + "\t名字" + s.getName());
}
}
// 方法2: Java8开始支持的Lambada表达式配合 Stream API 来进行分组排序
public static void fun2(ListStu stus) {
ListEntryDouble, ListStu list = stus.stream().collect(Collectors.groupingBy(Stu::getScore)).entrySet()
.stream().sorted((s1, s2) - -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
int index = 1;
for (EntryDouble, ListStu entry : list) {
System.out.print("名次:" + index + "\t分数:" + entry.getKey() + "\t名字");
entry.getValue().forEach((s) - System.out.print(" " + s.getName()));
System.out.println();
index++;
}
}
}
输出结果
名次:1 分数91.0 名字Sweet
名次:2 分数79.5 名字Tom
名次:2 分数79.5 名字Amdy
名次:2 分数79.5 名字Cherry
名次:3 分数68.0 名字Lucy
名次:4 分数65.0 名字Solem
名次:5 分数52.0 名字Jack
名次:5 分数52.0 名字Jerry
名次:1 分数:91.0 名字 Sweet
名次:2 分数:79.5 名字 Tom Amdy Cherry
名次:3 分数:68.0 名字 Lucy
名次:4 分数:65.0 名字 Solem
名次:5 分数:52.0 名字 Jack Jerry
---------------分割线---------------------
名次:1 分数:91.0 名字 Sweet
名次:2 分数:79.5 名字 Tom Amdy Cherry
名次:3 分数:68.0 名字 Lucy
名次:4 分数:65.0 名字 Solem
名次:5 分数:52.0 名字 Jack Jerry
相关问答
Q1: java中调用sort 使学生成绩由小到大排序 代码怎么写?
根据JAVA成绩排序名字代码你这要求JAVA成绩排序名字代码,只有一个办法JAVA成绩排序名字代码,但是有点约束JAVA成绩排序名字代码:
import java.util.Arrays;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
//注意JAVA成绩排序名字代码,只能用对象类型,不可以使用简单类型 如int[] num则报错
Integer[] num = {5,8,3,9,1};
//如果是num是List或 Set,则用Collections.sort(num,Collections.reverseOrder());
Arrays.sort(num,Collections.reverseOrder());
for(int i=0;inum.length;i++){
System.out.println(num[i]);
}
}
}
Q2: 设计一个给班级学生成绩排序的java程序,具体要求如下
按照题目要求编写的Java程序如下(注意 以下程序全部放在Main.java文件中)
class student{
String name;
int score;
public student(String name,int score){
this.name=name;
this.score=score;
}
String studentInfo(){
return "name="+this.name+",score="+this.score;
}
}
public class Main{
public static void main(String[] args){
student sty[]=new student[5];
sty[0]=new student("zhangsan",67);
sty[1]=new student("lisi",75);
sty[2]=new student("wangwu",57);
sty[3]=new student("zhaoliu",88);
sty[4]=new student("ruanqi",93);
student stu[]=new student[5];
for(int i=0;isty.length;i++){
stu[i]=sty[i];
}
for(int i=0;istu.length-1;i++){
for(int j=0;jstu.length-i-1;j++){
if(stu[j].scorestu[j+1].score){
student temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(int i=0;istu.length;i++){
System.out.println(stu[i].studentInfo());
}
}
}
Q3: 求java中类似学生信息管理系统中按学号,按姓名排序的代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sort {
public static void main(String[] args) {
Student p1 = new Student(1001, "小明", 20);
Student p2 = new Student(1002, "小红", 21);
Student p3 = new Student(1003, "小黑", 19);
ListStudent list = new ArrayListStudent();
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list, new ComparatorStudent() {
/*
* int compare(Student o1, Student o2) 返回一个基本类型的整型, 返回负数表示:o1 小于o2,
* 返回0 表示:o1和o2相等, 返回正数表示:o1大于o2。
*/
public int compare(Student o1, Student o2) {
// 按照学生的学号进行升序排列
if (o1.getId() o2.getId()) {
return 1;
}
if (o1.getId() == o2.getId()) {
return 0;
}
return -1;
}
});
write(list);
System.out.println("---------------------");
Collections.sort(list, new ComparatorStudent() {
/*
* int compare(Student o1, Student o2) 返回一个基本类型的整型, 返回负数表示:o1 小于o2,
* 返回0 表示:o1和o2相等, 返回正数表示:o1大于o2。
*/
public int compare(Student o1, Student o2) {
// 按照学生的年龄进行升序排列
if (o1.getAge() o2.getAge()) {
return 1;
}
if (o1.getAge() == o2.getAge()) {
return 0;
}
return -1;
}
});
write(list);
}
public static void write(ListStudent list) {
for (Student s : list) {
System.out.println(s.getId() + "\t" + s.getName() + "\t"
+ s.getAge());
}
}
}
public class Student {
private int id ;
private String name;
private int age;
//构造方法
public Student(int id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}
Q4: Java代码写一个分数排序,同分同名
ListInteger list = new ArrayList();
list.add(100);
list.add(100);
list.add(92);
list.add(90);
list.add(90);
list.add(80);
list.add(10);
for (Integer i : list) {
long count = list.stream().filter(integer - integer i).count() + 1;
System.out.println("分数:" + i + " 名次:" + count);
}
JAVA成绩排序名字代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java成绩排序名字代码怎么输入、JAVA成绩排序名字代码的信息别忘了在本站进行查找喔。







