> Java > java지도 시간 > 본문

Java는 온라인 시험 시스템의 시험 점수 및 순위 통계를 구현합니다.

WBOY
풀어 주다: 2023-09-25 09:09:15
원래의
1508명이 탐색했습니다.

Java는 온라인 시험 시스템의 시험 점수 및 순위 통계를 구현합니다.

Java는 온라인 시험 시스템의 시험 점수 및 순위 통계를 구현합니다.

현대 교육에서는 시험을 실시하고 학생의 능력을 평가하기 위해 온라인 시험 시스템을 사용하는 학교와 기관이 점점 더 많아지고 있습니다. 시험 점수를 보다 효율적으로 관리하고 분석하기 위한 중요한 기능은 학생의 시험 성적을 계산하고 순위를 매기는 것입니다. 이 기사에서는 Java를 사용하여 온라인 시험 시스템에 대한 시험 점수 및 순위 통계를 구현하는 방법을 보여 드리겠습니다.

먼저 시스템에는 Student, Exam, ExamScore, ScoreStatistics라는 메인 클래스가 포함되어야 하며, 데이터를 초기화하고 전체 시스템을 실행하는 메인 클래스(Main)가 포함되어야 한다는 점을 분명히 해야 합니다.

먼저 Student 클래스를 만들어 보겠습니다.

public class Student {
    private String name;
    private int studentId;
    
    public Student(String name, int studentId) {
        this.name = name;
        this.studentId = studentId;
    }
    
    // getter 和 setter 方法省略
    
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", studentId=" + studentId +
                '}';
    }
}
로그인 후 복사

Student 클래스에는 이름과 학생 번호라는 두 가지 속성이 있습니다. 생성자를 사용하여 학생 개체를 초기화하고 해당 getter 및 setter 메서드를 제공할 뿐만 아니라 학생 정보를 편리하게 출력하기 위해 재정의된 toString() 메서드를 제공합니다.

다음으로 시험 클래스(Exam)를 만듭니다.

public class Exam {
    private String subject;
    private List<ExamScore> scores;
    
    public Exam(String subject) {
        this.subject = subject;
        this.scores = new ArrayList<>();
    }
    
    public void addScore(ExamScore score) {
        scores.add(score);
    }
    
    // getter 和 setter 方法省略
    
    // 计算平均分方法
    public double calculateAverageScore() {
        double totalScore = 0;
        int count = 0;
        
        for (ExamScore score : scores) {
            totalScore += score.getScore();
            count++;
        }
        
        return totalScore / count;
    }
    
    @Override
    public String toString() {
        return "Exam{" +
                "subject='" + subject + ''' +
                ", scores=" + scores +
                '}';
    }
}
로그인 후 복사

시험 클래스에는 과목과 시험 점수 목록이라는 두 가지 속성이 있습니다. 우리는 생성자를 사용하여 시험 개체를 초기화하고 시험 점수를 추가하는 메서드와 해당 getter 및 setter 메서드를 제공합니다. 또한 시험의 평균 점수를 계산하고 toString() 메서드를 재정의하여 시험 정보를 출력하는 메서드도 제공합니다.

그런 다음 시험 점수 클래스(ExamScore)를 만듭니다.

public class ExamScore {
    private Student student;
    private double score;
    
    public ExamScore(Student student, double score) {
        this.student = student;
        this.score = score;
    }
    
    // getter 和 setter 方法省略
    
    @Override
    public String toString() {
        return "ExamScore{" +
                "student=" + student +
                ", score=" + score +
                '}';
    }
}
로그인 후 복사

시험 점수 클래스에는 학생과 점수라는 두 가지 속성이 있습니다. 생성자를 사용하여 테스트 점수 개체를 초기화하고 해당 getter 및 setter 메서드를 제공할 뿐만 아니라 재정의된 toString() 메서드를 제공하여 테스트 점수 정보를 출력합니다.

다음으로 점수 통계 클래스(ScoreStatistics)를 만듭니다.

public class ScoreStatistics {
    private List<ExamScore> scores;
    
    public ScoreStatistics(List<ExamScore> scores) {
        this.scores = scores;
    }
    
    public void printExamScores() {
        for (ExamScore score : scores) {
            System.out.println(score);
        }
    }
    
    public void printRanking() {
        Collections.sort(scores, Comparator.comparingDouble(ExamScore::getScore).reversed());
        
        int rank = 1;
        for (ExamScore score : scores) {
            System.out.println("Rank " + rank + ": " + score.getStudent().getName() + ", Score: " + score.getScore());
            rank++;
        }
    }
}
로그인 후 복사

점수 통계 클래스에는 점수 목록 속성이 있습니다. 생성자를 사용하여 통계 개체를 초기화하고 테스트 점수와 순위를 출력하는 두 가지 방법을 제공합니다. 순위를 출력할 때 성적 목록을 점수를 기준으로 내림차순으로 정렬하려면 Collections.sort() 메서드를 사용하고, 정렬 기준을 지정하려면 Comparator.comparingDouble() 메서드를 사용합니다. 그런 다음 루프를 사용하여 각 학생의 순위와 점수를 출력합니다.

마지막으로 데이터를 초기화하고 전체 시스템을 실행하기 위한 메인 클래스(Main)를 생성합니다.

public class Main {
    public static void main(String[] args) {
        // 初始化学生对象
        Student student1 = new Student("张三", 1001);
        Student student2 = new Student("李四", 1002);
        Student student3 = new Student("王五", 1003);
        
        // 初始化考试成绩对象
        ExamScore score1 = new ExamScore(student1, 80);
        ExamScore score2 = new ExamScore(student2, 90);
        ExamScore score3 = new ExamScore(student3, 85);
        
        // 初始化考试对象
        Exam exam = new Exam("数学");
        
        // 添加考试成绩到考试对象
        exam.addScore(score1);
        exam.addScore(score2);
        exam.addScore(score3);
        
        // 创建成绩统计对象
        List<ExamScore> scores = new ArrayList<>();
        scores.add(score1);
        scores.add(score2);
        scores.add(score3);
        ScoreStatistics statistics = new ScoreStatistics(scores);
        
        // 输出考试成绩和排名
        System.out.println("Exam Scores:");
        statistics.printExamScores();
        
        System.out.println();
        
        System.out.println("Exam Ranking:");
        statistics.printRanking();
    }
}
로그인 후 복사

메인 클래스에서는 먼저 학생 및 시험 점수 객체를 생성하고 다음을 통해 시험 점수를 테스트 객체에 추가합니다. 추가 방법. 그런 다음 점수 통계 개체를 만들고 이 개체를 사용하여 테스트 점수와 순위를 출력합니다.

이 프로그램을 실행하면 다음과 같은 출력이 표시됩니다.

Exam Scores:
ExamScore{student=Student{name='张三', studentId=1001}, score=80.0}
ExamScore{student=Student{name='李四', studentId=1002}, score=90.0}
ExamScore{student=Student{name='王五', studentId=1003}, score=85.0}

Exam Ranking:
Rank 1: 李四, Score: 90.0
Rank 2: 王五, Score: 85.0
Rank 3: 张三, Score: 80.0
로그인 후 복사

위는 Java를 사용하여 온라인 시험 시스템의 시험 점수 및 순위 통계를 구현하는 샘플 코드입니다. 이 시스템을 통해 학생들의 시험점수를 쉽게 관리, 분석하고, 점수에 따라 학생의 순위를 매길 수 있습니다. 물론 이는 단순한 예일 뿐이며 실제 필요에 따라 확장하고 최적화할 수 있습니다. 온라인 시험 시스템을 작성하는 데 행운이 있기를 바랍니다!

위 내용은 Java는 온라인 시험 시스템의 시험 점수 및 순위 통계를 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!