java实现学生信息统计系统

学生信息查询

功能:

1.通过id修改学生信息

2.获取指定学生的成绩

3.获取指定学号的成绩

4.根据姓名修改指定学科的成绩

5.删除指定学生及其成绩

6.统计所有成绩相等的人数

7.获取到所有学生某一科的成绩

代码块

package com.lanou.work01;

import java.util.ArrayList;
import java.util.HashSet;

public class ProgramSystem {

    public static void main(String[] args) {
        ArrayList<Student> listStudent = new ArrayList<Student>();

        // 学生小李
        Student student = new Student(1, "小李");
        Subject subject1 = new Subject("语文", 90.7);
        Subject subject2 = new Subject("数学", 10.5);
        student.list.add(subject1);
        student.list.add(subject2);

        // 学生小王
        Student student2 = new Student(1, "小王");
        Subject subject3 = new Subject("语文", 20.7);
        Subject subject4 = new Subject("数学", 10.5);
        student2.list.add(subject3);
        student2.list.add(subject4);

        // 装进大的数组
        listStudent.add(student);
        listStudent.add(student2);
        System.out.println(listStudent.size());

        Control control = new Control(listStudent);
        //以下注释内容为功能测试
        // control.getScore(student, "语文");
        // System.out.println("-------------------------");
        // control.getId(1, "数学");
        // System.out.println("-------------------------");
        // control.upDateName(1, "小陈");
        // System.out.println(student.nameString);
        // System.out.println("-------------------------");
        // control.upDateScore("小陈", "语文", 0.0);
        // System.out.println(student);
        // control.removeStudent(student);
        // System.out.println(listStudent);
        // control.countStudnet(student, "语文");
        control.fun("语文");
    }
}

class Control {
    // 存放学生的数组
    ArrayList<Student> listStudent = new ArrayList<Student>();

    public Control(ArrayList<Student> listStudent) {
        this.listStudent = listStudent;
    }
    //展示方法
    public void show(){
        System.out.println("*************************");
    }

    // 获取指定学生的成绩
    public void getScore(Student student, String bookName) {
        for (Student student2 : listStudent) {
            if (student.nameString.equals(student2.nameString)) {

                for (Subject sub : student.list) {
                    // 每一个成绩类对象
                    if (bookName.equals(sub.nameBook)) {
                        System.out.println(sub.Score);
                    }
                }
            }

        }
    }
    // 获取指定学号的成绩
    public void getId(int id, String bookName) {
        for (Student student2 : listStudent) {
            if (student2.id == id) {// 如果id相等话
                for (Subject sub : student2.list) {
                    // 每一个成绩类对象
                    if (bookName.equals(sub.nameBook)) {
                        System.out.println(sub.Score);
                    }
                }

            }

        }
    }

    // 根据id修改姓名
    public void upDateName(int id, String name) {
        for (Student student : listStudent) {
            if (student.id == id) {// 如果想等的话
                student.nameString = name;
            }
        }
    }

    // 根据姓名修改指定学科的成绩
    public void upDateScore(String name, String bookName, Double score) {
        for (Student student : listStudent) {
            if (student.nameString.equals(name)) {
                for (Subject subject : student.list) {
                    if (subject.nameBook.equals(bookName)) {
                        subject.Score = score;
                    }
                }
            }
        }
    }
    // 删除指定学生及其成绩
    public void removeStudent(Student student) {
        Student student3 = new Student();
        System.out.println(listStudent);
        for (Student student2 : listStudent) {
            if (student2.nameString.equals(student.nameString)) {
                student3 = student2;
            }
        }
        listStudent.remove(student3);
    }

    // 统计所有成绩相等的人数
    public void countStudnet(Student student, String namString) {
        int count = 1;
        // 创建一个数组
        double array[] = new double[listStudent.size()];
        System.out.println(listStudent.size());
        for (Student student2 : listStudent) {
            for (int i = 0; i < array.length; i++) {
                System.out.println("数组的长度"+array.length);
                for (Subject subject : student2.list) {
                    if (subject.nameBook.equals(namString)) {
                        System.out.println(namString);
                        array[i] = subject.Score;
                        System.out.println(array[i]);
                    }
                }
            }
        }
        // 第一个和第二个比较
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] == array[j]) {
                    count++;
                }
            }
        }
        System.out.println("语文成绩相等的人数为:" + count);

    }

    // 获取到所有学生的成绩
    public void fun(String nameString) {
        HashSet<Double> set = new HashSet<Double>();
        for (Student student2 : listStudent) {
                    for (Subject subject : student2.list) {
                         if (nameString.equals(subject.nameBook)) {
                            set.add(subject.Score); 
                        }   
                    }   
            }
        System.out.println(set);
    }
}
//学生类
class Student {

    String nameString;
    int id;
    ArrayList<Subject> list = new ArrayList<Subject>();

    public Student(int id, String nameString) {
        this.id = id;
        this.nameString = nameString;

    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        for (Subject subject : list) {
            return "姓名:" + nameString + "学号:" + id + "成绩单:" + subject.nameBook
                    + subject.Score;
        }
        return null;

    }

    public Student() {

    }
}

// 学科类
class Subject {

    String nameBook;
    double Score;

    public Subject(String nameBook, double score) {
        super();
        this.nameBook = nameBook;
        Score = score;
    }

}
发布了40 篇原创文章 · 获赞 10 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览