6-1 成绩管理系统 (30 分)
构造一个成绩管理系统CourseManagementSystem,该系统包括如下几个方法:void add(int no, int grade)添加该学号的成绩,如果系统已有该学生成绩,则输出"the student already exists";void delete(int no)删除某学号成绩,如果不存在此学生则输出"no such student";int query(int no)查询并返回该学号的成绩;统计成绩void statistics( )统计[0-59]、[60-69]、[70-79]、[80-89]、[90-100]各成绩段的学生个数并打印。请选择合适的容器实现上述功能。(题目假设不会重复添加相同学号的学生成绩) main函数中读入操作类型及相关参数,并调用statictic函数输出学生成绩统计信息。
输入描述:
操作个数 操作名 操作参数
输出描述:
查询学生的成绩 各成绩段的学生个数
裁判测试程序样例:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CourseManagementSystem cms = new CourseManagementSystem();
int ops = sc.nextInt();
for (int i=0;i<ops;i++) {
String op = sc.next();
if (op.equals("add"))
cms.add(sc.nextInt(), sc.nextInt());
else if (op.equals("delete"))
cms.delete(sc.nextInt());
else if (op.equals("query")) {
int no = sc.nextInt();
int s = cms.query(no);
System.out.println("the score for "+no+" is : "+s);
}
}
cms.statistic();
}
}
/* 你的代码被嵌在这里*/
输入样例:
在这里给出一组输入。例如:
8
add 1 63
add 2 78
add 3 74
delete 3
add 2 20
delete 5
query 1
add 4 90
输出样例:
在这里给出相应的输出。例如:
the student already exists
no such student
the score for 1 is : 63
[0-59] : 0
[60-69] : 1
[70-79] : 1
[80-89] : 0
[90-100] : 1
代码
class CourseManagementSystem {
public int no;
public int socre;
TreeMap<Integer, Integer> tMap = new TreeMap<>();
public void add(int no, int score) {
// TODO Auto-generated method stub
if (tMap.containsKey(no)) {
System.out.println("the student already exists");
} else {
tMap.put(no, score);
}
}
public void delete(int no) {
// TODO Auto-generated method stub
if (tMap.containsKey(no)) {
tMap.remove(no);
} else {
System.out.println("no such student");
}
}
public int query(int no) {
// TODO Auto-generated method stub
return tMap.get(no);
}
public void statistic() {
// TODO Auto-generated method stub
int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0;
for (Map.Entry<Integer, Integer> entry : tMap.entrySet()) {
if (entry.getValue() >= 90 && entry.getValue() <= 100) {
f1++;
} else if (entry.getValue() >= 80) {
f2++;
} else if (entry.getValue() >= 70) {
f3++;
} else if (entry.getValue() >= 60) {
f4++;
} else {
f5++;
}
}
System.out.println("[0-59] : " + f5);
System.out.println("[60-69] : " + f4);
System.out.println("[70-79] : " + f3);
System.out.println("[80-89] : " + f2);
System.out.println("[90-100] : " + f1);
}
}