利用JAVA编写程序 要求:(1)按班级按课程从文件中读入相应的平时成绩、期中考试成绩和期末考试成绩。 (2)三个成绩对总评成绩的百分比被定义为常数,各占总成绩的30%、30%和40%。(3)计算每位学生的总评成绩。 (4)计算该班级本课程的总平均成绩。 (5)计算处于优、良、中、及格、不及格的学生人数以及占总人数的百分比。其中100-90为优,89-80为良,79-70为中,69-60为及格,60分以下为不及格。 (6)按要求输出成绩在优、良、中、及格、不及格各区间的学生学号、成绩。
时间: 2024-03-28 15:40:42 浏览: 152
基于java+swing+TXT文本实现学生成绩管理系统(高分课程设计)
以下是JAVA程序的实现,可以参考:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ScoreSystem {
private static final double WEIGHT1 = 0.3;
private static final double WEIGHT2 = 0.3;
private static final double WEIGHT3 = 0.4;
private Map<String, Map<String, List<Double>>> scores = new HashMap<>();
public void loadScores(String filename) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
String classname = fields[0];
String course = fields[1];
double score1 = Double.parseDouble(fields[2]);
double score2 = Double.parseDouble(fields[3]);
double score3 = Double.parseDouble(fields[4]);
scores.computeIfAbsent(classname, k -> new HashMap<>())
.computeIfAbsent(course, k -> new ArrayList<>())
.add(score1);
scores.get(classname).get(course).add(score2);
scores.get(classname).get(course).add(score3);
}
}
}
public void calculateScores() {
for (String classname : scores.keySet()) {
for (String course : scores.get(classname).keySet()) {
List<Double> scoresList = scores.get(classname).get(course);
double sum1 = 0;
double sum2 = 0;
double sum3 = 0;
for (int i = 0; i < scoresList.size(); i += 3) {
sum1 += scoresList.get(i);
sum2 += scoresList.get(i + 1);
sum3 += scoresList.get(i + 2);
}
double avg1 = sum1 / (scoresList.size() / 3);
double avg2 = sum2 / (scoresList.size() / 3);
double avg3 = sum3 / (scoresList.size() / 3);
double totalScore = avg1 * WEIGHT1 + avg2 * WEIGHT2 + avg3 * WEIGHT3;
scores.get(classname).get(course).add(totalScore);
}
}
}
public void printScores(String classname, String course) {
List<Double> scoresList = scores.get(classname).get(course);
List<String> students = new ArrayList<>();
Map<String, Double> scoreMap = new HashMap<>();
for (int i = 0; i < scoresList.size() - 1; i += 4) {
String student = String.format("%03d", (i / 4) + 1);
double score = scoresList.get(i + 3);
students.add(student);
scoreMap.put(student, score);
}
Collections.sort(students, (s1, s2) -> Double.compare(scoreMap.get(s2), scoreMap.get(s1)));
System.out.println("学号\t成绩");
for (String student : students) {
double score = scoreMap.get(student);
System.out.println(student + "\t" + score);
}
}
public void printSummary(String classname, String course) {
System.out.println("班级:" + classname);
System.out.println("课程:" + course);
List<Double> scoresList = scores.get(classname).get(course);
double sum = 0;
int count = 0;
int excellentCount = 0;
int goodCount = 0;
int mediumCount = 0;
int passCount = 0;
int failCount = 0;
for (int i = 0; i < scoresList.size() - 1; i += 4) {
double score = scoresList.get(i + 3);
sum += score;
count++;
if (score >= 90) {
excellentCount++;
} else if (score >= 80) {
goodCount++;
} else if (score >= 70) {
mediumCount++;
} else if (score >= 60) {
passCount++;
} else {
failCount++;
}
}
double avg = sum / count;
double excellentPercent = (double) excellentCount / count * 100;
double goodPercent = (double) goodCount / count * 100;
double mediumPercent = (double) mediumCount / count * 100;
double passPercent = (double) passCount / count * 100;
double failPercent = (double) failCount / count * 100;
System.out.println("总平均成绩:" + avg);
System.out.println("优秀人数:" + excellentCount + ",占比:" + excellentPercent + "%");
System.out.println("良好人数:" + goodCount + ",占比:" + goodPercent + "%");
System.out.println("中等人数:" + mediumCount + ",占比:" + mediumPercent + "%");
System.out.println("及格人数:" + passCount + ",占比:" + passPercent + "%");
System.out.println("不及格人数:" + failCount + ",占比:" + failPercent + "%");
}
public static void main(String[] args) throws IOException {
ScoreSystem system = new ScoreSystem();
system.loadScores("scores.txt");
system.calculateScores();
system.printScores("class1", "course1");
system.printSummary("class1", "course1");
}
}
```
这个程序实现了从文件中读取学生成绩数据、按照权重计算总评成绩、计算班级平均成绩、计算各区间人数和占比、输出成绩在某个区间的学生学号和成绩等功能。可以根据自己的需求进行修改和扩展。
阅读全文