「java总成绩排序」java把学生类按成绩排序

博主:adminadmin 2023-01-04 00:33:08 563

今天给各位分享java总成绩排序的知识,其中也会对java把学生类按成绩排序进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java根据学生总分按顺序排名,怎么调用排序?无法应用。求大神修改!

import java.util.Arrays;

public class Student {

String name,number;

double s1,s2,s3,sum;

//student方法,返回值为一个学生3门课程分数之和

public double Student(String n,String no,double x1,double x2,double x3){

name=n;

number=no;

s1=x1;

s2=x2;

s3=x3;

return s1+s2+s3;

}

//main方法

public static void main(String args[]){

Student stu=new Student();//实例化对象

double[] s=new double[5];

//调用stu方法,将分数和给一个double数组

s[0]=stu.Student("Zhangsan","20140001",89,87,90);

s[1]=stu.Student("Lisi","20140002",88,87,90);

s[2]=stu.Student("Wangwu","20140003",89,80,90);

s[3]=stu.Student("Zhaohong","20140004",89,87,78);

s[4]=stu.Student("Gaozi","20140005",81,87,90);

Arrays.sort(s);//排序方法,从小到大

for(int i=0;i5;i++){

System.out.println(s[i]);

}

}

}

*******************************************

有注释,应该能看懂

java语言对几个同学的成绩进行排序

package test;

import java.util.Scanner;

public class test{

public static void main(String[] args)

{

test ts = new test();

Scanner sc = new Scanner(System.in); //Scanner类的构造方法。

int a[] = new int[10];

System.out.println("输入10个同学的成绩,中间以空格隔开");

for (int i = 0; i a.length; i++) {

a[i] = sc.nextInt(); //依次输入数字中间以空格隔开。

}

test.maopao(a); //调用排序方法。

for (int i = 0; i a.length; i++)

System.out.println(a[i]);

}

public static void maopao(int array[]) { //定义冒泡排序方法。

int i;

int k;

for (i = 0; i array.length; i++) {

for (k = 0; k array.length - 1 - i; k++) {

if (array[k] array[k + 1]) {

int temp = array[k];

array[k] = array[k + 1];

array[k + 1] = temp;

}

}

}

}

}

设计一个给班级学生成绩排序的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());

}

}

}

这串JAVA代码如何实现按科目成绩排序啊?

排序可以使用Collections.sort。

例子如下:

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Main { private static class Student{ private String name; private float grade; public Student(String name, float grade) { this.name = name; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getGrade() { return grade; } public void setGrade(float grade) { this.grade = grade; } @Override public String toString() { return name + " " + grade; } } public static void main(String[] args) { ListStudent students = new ArrayList(); students.add(new Student("Tom", 86)); students.add(new Student("Jack", 70)); students.add(new Student("Mary", 90)); Collections.sort(students, new ComparatorStudent() { @Override public int compare(Student o1, Student o2) { if (o1.getGrade() - o2.getGrade() 0) { return 1; } else if (o1.getGrade() - o2.getGrade() 0) { return -1; } else { return 0; } } }); for (Student student : students) { System.out.println(student.toString()); } }}

使用要排序的字段来比较。就可以得到有序的列表。

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

关于java总成绩排序和java把学生类按成绩排序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。