2 Star 0 Fork 0

BESTI.IS.JAVA2018/20165202jhs

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Student.java 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
Haisu_20165202 提交于 2018-05-06 11:44 +08:00 . 第十周测试:排序
import java.util.*;
class StudentKey implements Comparable {
double d=0;
String s="";
StudentKey (double d) {
this.d=d;
}
StudentKey (String s) {
this.s=s;
}
public int compareTo(Object b) {
StudentKey st=(StudentKey)b;
if((this.d-st.d)==0)
return -1;
else
return (int)((this.d-st.d)*1000);
}
}
class StudentIF {
String name=null;
int ID=0;
double math,english,computer,total,aver;
StudentIF(String s, double m, double e, double f, double a,double b,int c) {
name=s;
math=m;
english=e;
computer=f;
total=a;
aver=b;
ID=c;
}
}
public class Student {
public static void main(String args[]) {
TreeMap<StudentKey, StudentIF> treemap = new TreeMap<StudentKey, StudentIF>();
String str[] = {"其米仁增","李梓豪", "贾海粟", "夏云霄", "贾普涵"};
int ID[] ={20165239,20165201,20165202,20165203,20165204};
double math[] = {96, 77, 63, 89, 96};
double english[] = {87, 70, 68, 86, 92};
double computer[] = {97, 66, 73, 66, 91};
double total[] = new double[5];
double aver[] = new double[5];
StudentIF student1[] = new StudentIF[5];
StudentIF student2[] = new StudentIF[5];
for (int k = 0; k < student1.length; k++) {
total[k] = math[k] + english[k] + computer[k];
aver[k] = total[k] / 3;
}
for (int k = 0; k < student1.length; k++) {
student1[k] = new StudentIF(str[k], math[k], english[k], computer[k], total[k], aver[k], ID[k]);
}
StudentKey key[] = new StudentKey[5];
student2= student1;
for (int k = 0; k < key.length; k++) {
key[k] = new StudentKey(student1[k].ID);
}
for (int k = 0; k < student1.length; k++) {
treemap.put(key[k], student1[k]);
}
for (int k = 0; k < key.length; k++) {
key[k] = new StudentKey(student1[k].total);
}
for (int k = 0; k < student1.length; k++) {
treemap.put(key[k], student1[k]);
}
int number = treemap.size();
System.out.println("学生共有" + number + "个,按总成绩排序:");
Collection<StudentIF> collection = treemap.values();
Iterator<StudentIF> iter = collection.iterator();
int i=0;
while (iter.hasNext()) {
StudentIF stu = iter.next();
System.out.println(" 学号 " + stu.ID + " 姓名 " + stu.name + " 总成绩 " + stu.total);
i++;
if(i==5)
System.out.println("学生共有" + number + "个,按学号排序:");
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/BESTI-IS-JAVA-2018/20165202jhs.git
git@gitee.com:BESTI-IS-JAVA-2018/20165202jhs.git
BESTI-IS-JAVA-2018
20165202jhs
20165202jhs
master

搜索帮助