6-1 成绩管理系统 (30 分)

本文介绍了如何使用Java编程实现一个基于TreeMap的成绩管理系统。系统包括添加、删除、查询成绩功能,以及统计各成绩段的学生数量。在main函数中,通过用户输入的操作指令来执行相应操作,并最终输出成绩统计信息。示例展示了系统的具体实现和测试用例。
摘要由CSDN通过智能技术生成

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);
	}

}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值