📄 students.java
字号:
import java.util.*;
class Students {
//增加
public void addStudent(String id, String name,float sc) {
stuList.add(new Student(id,name,sc));
}
//删除元素(对于LinkedList建议使用迭代器遍历删除)
public void deleteStudent(String id) {
it=stuList.listIterator();
while (it.hasNext()){
s=(Student)it.next();
if (s.ID.equals(id)) {
it.remove();
break;
}
}
}
//编辑分数
public void setScore(String id, float sc) {
it=stuList.listIterator();
while (it.hasNext()){
s=(Student)it.next();
if (s.ID.equals(id)) {
s.score= sc;
break;
}
}
}
//求平均成绩
public float averageScore () {
int i=0;
float avg=0f;
float sum=0f;
it=stuList.listIterator();
while (it.hasNext()){
s=(Student)it.next();
sum+=s.score;
i++;
}
avg=sum/i;
return avg;
}
//统计优秀、良好、中等和不及格的人数比例。
public void stat(float[] sc) {
int a=0,b=0,c=0,d=0;//计数器
int sum=0;
float curScore=0f;
it=stuList.listIterator();
while (it.hasNext()){
s=(Student)it.next();
curScore=s.score;
//优秀(100-90)、良好(89-75)、中等(74-60)、不及格(59-0)
if (curScore>89) {a++;}
else if (curScore>74) {b++;}
else if (curScore>59) {c++;}
else {d++;}
}
sum=a+b+c+d;
//转化为百分比
sc[0]=100f*a/sum;
sc[1]=100f*b/sum;
sc[2]=100f*c/sum;
sc[3]=100f-sc[0]-sc[1]-sc[2];
}
//按学号查找
public Student seek(String id) {
s=null;
it=stuList.listIterator();
while (it.hasNext()){
s=(Student)it.next();
if (s.ID.equals(id)) break; else s=null;
}
return s;
}
//排序
public List sortByScore() {
//冒泡排序: 按照分数降序排列数组元素
Student a=null,b=null; // 用于临时存放交换数据的变量
for (int pass = 1; pass < stuList.size(); pass++){ // 外层循环
for (int pair = 1; pair < (stuList.size()-pass+1); pair++) {
// 内层循环
a=(Student)(stuList.get(pair-1));
b=(Student)(stuList.get(pair));
if (a.score < b.score ) { // 比较大小
// 交换位置
stuList.set(pair-1,b);
stuList.set(pair,a);
}
}
}
return stuList;
}
public List stuList=new ArrayList();
private ListIterator it;
private Student s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -