用java写一个学生类,对总成绩降序排列输出并打印名次

z980693651 2017-11-12 09:58:50
要求:编写一个学生类,输入姓名,学号,三门课成绩,然后按总分降序排列输出,并打印名次,如果总分相同,输出同样的名次,如1 2 2 3 4
刚开始学java,只能写到下面那样了,输出名次完全没有思路,排序也感觉怪怪的,有没有大佬能够指点一下QAQ

import java.util.Arrays;
public class Student {
public String name;
public int xuehao,mingci;
public double a1,a2,a3;
Student(int xuehao,String name,double a1,double a2,double a3){
this.xuehao=xuehao;
this.name=name;
this.a1=a1;
this.a2=a2;
this.a3=a3;
}
double sum(){
return(this.a1+this.a2+this.a3);
}
}
public class Test {
public static void main(String[] args){
Student student[]=new Student[5];
double s[]=new double[5];
student[0]=new Student(20160001,"学生一",85,72,95);
student[1]=new Student(20160002,"学生二",87,88,92);
student[2]=new Student(20160003,"学生三",20,60,56);
student[3]=new Student(20160004,"学生四",99,94,56);
student[4]=new Student(20160005,"学生五",100,100,100);
for(int j=0;j<5;j++){
s[j]=student[j].sum();
}
Arrays.sort(s);
for(int j=4;j>=0;j--){
for(int i=0;i<5;i++){
if(student[i].sum()==s[j])
System.out.println(student[i].xuehao+" "+student[i].name+" "+student[i].a1+" "+
student[i].a2+" "+student[i].a3+" 总分:"+student[i].sum());
}
}
}
}
...全文
3780 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2017-11-13
  • 打赏
  • 举报
回复
public class Test3 {
	public static void main(String[] args) {
		Student[] student = new Student[8];
		student[0] = new Student(20160001, "学生一", 85, 72, 95);
		student[1] = new Student(20160002, "学生二", 87, 72, 93);
		student[2] = new Student(20160003, "学生三", 88, 73, 91);
		student[3] = new Student(20160004, "学生四", 99, 94, 56);
		student[4] = new Student(20160005, "学生五", 100, 100, 100);
		student[5] = new Student(20160006, "学生六", 35, 78, 83);
		student[6] = new Student(20160007, "学生七", 87, 66, 78);
		student[7] = new Student(20160008, "学生八", 87, 66, 78);
		Student temp; //排序使用的临时对象
		//按sum排序
		for (int i = 0; i < student.length; i++) {
			for (int j = i+1; j < student.length; j++) {
				if(student[j].sum() > student[i].sum()){
					temp = student[i];
					student[i] = student[j];
					student[j] = temp;
				}
			}		
		}
		int mingci = 1;//名次
		
		for (int i = 0; i < student.length-1; i++) {
			int n = checkContinue(student, student[i].sum());
			if (n == 1) {
				student[i].mingci = mingci++;
			}else {
				//总分相同,名次相同
				for (int j = 0; j < n; j++) {
					student[i+j].mingci = mingci;				
				}
				mingci++;
				i = i + n -1;//连续n个相同的总分,排名一样
			}
		}
		student[student.length-1].mingci = mingci;
		for (int i = 0; i < student.length; i++) {
			System.out.println(student[i]) ;
		}
	}
	//判断是否连续
	public static int checkContinue(Student[] student,double sum){
		int count = 0 ;//统计多少个连续相同的sum
		for (int i = 0; i < student.length; i++) {
			if(student[i].sum() == sum){
				count++;
			}
		}
		return count;
	}
}
class Student {
	public String name;
	public int xuehao, mingci;
	public double a1, a2, a3;

	Student(int xuehao, String name, double a1, double a2, double a3) {
		this.xuehao = xuehao;
		this.name = name;
		this.a1 = a1;
		this.a2 = a2;
		this.a3 = a3;
	}

	double sum() {
		return (this.a1 + this.a2 + this.a3);
	}
	
	public String toString(){
		return "名次:" + this.mingci + " 学号:" + this.xuehao+"  "+this.name+" "+this.a1+" "+
				this.a2+" "+this.a3+" 总分:"+this.sum();
	}
}
名次:1 学号:20160005 学生五 100.0 100.0 100.0 总分:300.0 名次:2 学号:20160002 学生二 87.0 72.0 93.0 总分:252.0 名次:2 学号:20160003 学生三 88.0 73.0 91.0 总分:252.0 名次:2 学号:20160001 学生一 85.0 72.0 95.0 总分:252.0 名次:3 学号:20160004 学生四 99.0 94.0 56.0 总分:249.0 名次:4 学号:20160007 学生七 87.0 66.0 78.0 总分:231.0 名次:4 学号:20160008 学生八 87.0 66.0 78.0 总分:231.0 名次:5 学号:20160006 学生六 35.0 78.0 83.0 总分:196.0
繁华终归落尽 2017-11-13
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable<Student>{
	public String name;
	public int xuehao, mingci;
	public double a1, a2, a3;

	public Student(int xuehao, String name, double a1, double a2, double a3) {
		this.xuehao = xuehao;
		this.name = name;
		this.a1 = a1;
		this.a2 = a2;
		this.a3 = a3;
	}

	public double sum() {
		return (this.a1 + this.a2 + this.a3);
	}

	@Override
	public int compareTo(Student o) {
		double o1Sum = this.sum();
		double o2Sum = o.sum();
		return o1Sum == o2Sum ? 0 : o1Sum > o2Sum ? 1 : -1;
	}
	
	public static void main(String[] args) {
		List<Student> students = new ArrayList<Student>();
		students.add(new Student(20160001,"学生一",85,72,95));
		students.add(new Student(20160002,"学生二",87,88,92));
		students.add(new Student(20160003,"学生三",20,60,56));
		students.add(new Student(20160004,"学生四",99,94,59));
		students.add(new Student(20160005,"学生五",100,100,100));
		System.out.println("初始顺序(姓名,总分)");
		for (Student p : students) {
            System.out.println(p.name + " | " + p.sum());
        }
		
		Collections.sort(students);
		
		System.out.println("排序后(姓名,总分,名次)");
		int index = 0;
		for (int i = 0; i < students.size(); i++) {
			Student p = students.get(i);
			if(i == 0 || p.sum() > students.get(i-1).sum()){
				index ++ ;
			}
            System.out.println(p.name + " | " + p.sum() + " | " + index);
        }
	}

}
bug木有了 2017-11-13
  • 打赏
  • 举报
回复
你这写的什么乱七八糟的一堆啊
z980693651 2017-11-13
  • 打赏
  • 举报
回复
引用 3 楼 yuxiangaaaaa 的回复:
public class Test3 {
	public static void main(String[] args) {
		Student[] student = new Student[8];
		student[0] = new Student(20160001, "学生一", 85, 72, 95);
		student[1] = new Student(20160002, "学生二", 87, 72, 93);
		student[2] = new Student(20160003, "学生三", 88, 73, 91);
		student[3] = new Student(20160004, "学生四", 99, 94, 56);
		student[4] = new Student(20160005, "学生五", 100, 100, 100);
		student[5] = new Student(20160006, "学生六", 35, 78, 83);
		student[6] = new Student(20160007, "学生七", 87, 66, 78);
		student[7] = new Student(20160008, "学生八", 87, 66, 78);
		Student temp; //排序使用的临时对象
		//按sum排序
		for (int i = 0; i < student.length; i++) {
			for (int j = i+1; j < student.length; j++) {
				if(student[j].sum() > student[i].sum()){
					temp = student[i];
					student[i] = student[j];
					student[j] = temp;
				}
			}		
		}
		int mingci = 1;//名次
		
		for (int i = 0; i < student.length-1; i++) {
			int n = checkContinue(student, student[i].sum());
			if (n == 1) {
				student[i].mingci = mingci++;
			}else {
				//总分相同,名次相同
				for (int j = 0; j < n; j++) {
					student[i+j].mingci = mingci;				
				}
				mingci++;
				i = i + n -1;//连续n个相同的总分,排名一样
			}
		}
		student[student.length-1].mingci = mingci;
		for (int i = 0; i < student.length; i++) {
			System.out.println(student[i]) ;
		}
	}
	//判断是否连续
	public static int checkContinue(Student[] student,double sum){
		int count = 0 ;//统计多少个连续相同的sum
		for (int i = 0; i < student.length; i++) {
			if(student[i].sum() == sum){
				count++;
			}
		}
		return count;
	}
}
class Student {
	public String name;
	public int xuehao, mingci;
	public double a1, a2, a3;

	Student(int xuehao, String name, double a1, double a2, double a3) {
		this.xuehao = xuehao;
		this.name = name;
		this.a1 = a1;
		this.a2 = a2;
		this.a3 = a3;
	}

	double sum() {
		return (this.a1 + this.a2 + this.a3);
	}
	
	public String toString(){
		return "名次:" + this.mingci + " 学号:" + this.xuehao+"  "+this.name+" "+this.a1+" "+
				this.a2+" "+this.a3+" 总分:"+this.sum();
	}
}
名次:1 学号:20160005 学生五 100.0 100.0 100.0 总分:300.0 名次:2 学号:20160002 学生二 87.0 72.0 93.0 总分:252.0 名次:2 学号:20160003 学生三 88.0 73.0 91.0 总分:252.0 名次:2 学号:20160001 学生一 85.0 72.0 95.0 总分:252.0 名次:3 学号:20160004 学生四 99.0 94.0 56.0 总分:249.0 名次:4 学号:20160007 学生七 87.0 66.0 78.0 总分:231.0 名次:4 学号:20160008 学生八 87.0 66.0 78.0 总分:231.0 名次:5 学号:20160006 学生六 35.0 78.0 83.0 总分:196.0
哇很感谢大神
李德胜1995 2017-11-13
  • 打赏
  • 举报
回复
引用 3 楼 yuxiangaaaaa 的回复:
public class Test3 {
	public static void main(String[] args) {
		Student[] student = new Student[8];
		student[0] = new Student(20160001, "学生一", 85, 72, 95);
		student[1] = new Student(20160002, "学生二", 87, 72, 93);
		student[2] = new Student(20160003, "学生三", 88, 73, 91);
		student[3] = new Student(20160004, "学生四", 99, 94, 56);
		student[4] = new Student(20160005, "学生五", 100, 100, 100);
		student[5] = new Student(20160006, "学生六", 35, 78, 83);
		student[6] = new Student(20160007, "学生七", 87, 66, 78);
		student[7] = new Student(20160008, "学生八", 87, 66, 78);
		Student temp; //排序使用的临时对象
		//按sum排序
		for (int i = 0; i < student.length; i++) {
			for (int j = i+1; j < student.length; j++) {
				if(student[j].sum() > student[i].sum()){
					temp = student[i];
					student[i] = student[j];
					student[j] = temp;
				}
			}		
		}
		int mingci = 1;//名次
		
		for (int i = 0; i < student.length-1; i++) {
			int n = checkContinue(student, student[i].sum());
			if (n == 1) {
				student[i].mingci = mingci++;
			}else {
				//总分相同,名次相同
				for (int j = 0; j < n; j++) {
					student[i+j].mingci = mingci;				
				}
				mingci++;
				i = i + n -1;//连续n个相同的总分,排名一样
			}
		}
		student[student.length-1].mingci = mingci;
		for (int i = 0; i < student.length; i++) {
			System.out.println(student[i]) ;
		}
	}
	//判断是否连续
	public static int checkContinue(Student[] student,double sum){
		int count = 0 ;//统计多少个连续相同的sum
		for (int i = 0; i < student.length; i++) {
			if(student[i].sum() == sum){
				count++;
			}
		}
		return count;
	}
}
class Student {
	public String name;
	public int xuehao, mingci;
	public double a1, a2, a3;

	Student(int xuehao, String name, double a1, double a2, double a3) {
		this.xuehao = xuehao;
		this.name = name;
		this.a1 = a1;
		this.a2 = a2;
		this.a3 = a3;
	}

	double sum() {
		return (this.a1 + this.a2 + this.a3);
	}
	
	public String toString(){
		return "名次:" + this.mingci + " 学号:" + this.xuehao+"  "+this.name+" "+this.a1+" "+
				this.a2+" "+this.a3+" 总分:"+this.sum();
	}
}
名次:1 学号:20160005 学生五 100.0 100.0 100.0 总分:300.0 名次:2 学号:20160002 学生二 87.0 72.0 93.0 总分:252.0 名次:2 学号:20160003 学生三 88.0 73.0 91.0 总分:252.0 名次:2 学号:20160001 学生一 85.0 72.0 95.0 总分:252.0 名次:3 学号:20160004 学生四 99.0 94.0 56.0 总分:249.0 名次:4 学号:20160007 学生七 87.0 66.0 78.0 总分:231.0 名次:4 学号:20160008 学生八 87.0 66.0 78.0 总分:231.0 名次:5 学号:20160006 学生六 35.0 78.0 83.0 总分:196.0

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧