今天这期blog是关于对之前发布的6-8次PTA题目集(成绩计算系列)进行的总结分析,但由于本人学识浅薄,只完成了部分代码
这是第一次课程成绩统计程序
以下为代码
import java.util.Scanner;
import java.text.Collator;
import java.util.Locale;
import java.util.Arrays;
class Course{
String CourseName;
String type;
String way;
int dailyScore=0;
int finalScore=0;
int studentNum=0;
void addScore(ExamGrade examGrade)
{
finalScore+=examGrade.finalGrade;
dailyScore+=examGrade.dailyGrade;
studentNum=studentNum+1;
}
void addScore(InvestGrade investigateGrade)
{
finalScore+=investigateGrade.finalGrade;
studentNum=studentNum+1;
}
int getAveragedailyScore()
{
return (int)(dailyScore/studentNum);
}
int getAverageFinalScore()
{
return (int)(finalScore/studentNum);
}
int getAverageScore()
{
if(type.equals("必修"))
return (int)(getAveragedailyScore()*0.3+getAverageFinalScore()*0.7);
else if(type.equals("选修")&&way.equals("考试"))
return (int)(getAveragedailyScore()*0.3+getAverageFinalScore()*0.7);
else if(type.equals("选修")&&way.equals("考察"))
return getAverageFinalScore();
return 0;
}
}
class Student{
String classNum;
String name;
Subject[] subject=new Subject[100];
int y=0;
int getTotalGrade()
{
int totalGrade=0;
for(int i=0;i<y;i++) {
totalGrade+=subject[i].grade.getGrade();
}
if(y!=0) return totalGrade/y;
return -1;
}
void addSubject(String SubjectName,Grade grade) {
subject[y]=new Subject();
subject[y].SubjectName = SubjectName;
subject[y].grade = grade;
y++;
}
Subject findSubject(String SubjectName)
{
for(int i=0;i<y;i++)
{
if(subject[i].SubjectName.equals(SubjectName))
return subject[i];
}
return null;
}
}
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Record record=new Record();
String str = in.nextLine();
while (!str.equals("end"))
{
String[] data=str.split(" ");
if(data.length==3&&data[1].equals("选修")&&(data[2].equals("考试")||data[2].equals("考察")))
{
record.addACourse(data[0],data[1],data[2]);
}
else if(data.length==3&&data[1].equals("必修")&&data[2].equals("考试"))
{
record.addACourse(data[0],data[1],data[2]);
}
else if(data.length==3&&data[1].equals("必修")&&data[2].equals("考察"))
{
System.out.println(data[0] + " : " + "course type & access mode mismatch");
}
else if(data.length==4||data.length==5)
{
if(record.findACourse(data[2])!=null)
{
if(record.findACourse(data[2]).type.equals("必修")&&data.length==5)
{
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4]));
if(Integer.parseInt(data[3])<=100&&data[1].length()<=10&&data[2].length()<=10&&Integer.parseInt(data[3])>=0&&data[0].length()==8&&Integer.parseInt(data[4])<=100&&Integer.parseInt(data[4])>=0) {
record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade);
record.findACourse(data[2]).addScore((ExamGrade) grade);
}
else System.out.println("wrong format");
}
else if(record.findACourse(data[2]).way.equals("考察")&&record.findACourse(data[2]).type.equals("选修")&&data.length==4)
{
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
Grade grade = new InvestGrade();
grade.finalGrade=Integer.parseInt(data[3]);
if(Integer.parseInt(data[3])<=100&&data[0].length()==8&&data[1].length()<=10&&data[2].length()<=10&&Integer.parseInt(data[3])>=0) {
record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade);
record.findACourse(data[2]).addScore((InvestGrade)grade);
}
else System.out.println("wrong format");
}
else if(record.findACourse(data[2]).type.equals("选修")&&data.length==5&&record.findACourse(data[2]).way.equals("考试"))
{
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
Grade grade=new ExamGrade(Integer.parseInt(data[3]),Integer.parseInt(data[4]));
if(Integer.parseInt(data[3])<=100&&data[0].length()==8&&data[1].length()<=10&&data[2].length()<=10&&Integer.parseInt(data[3])>=0&&Integer.parseInt(data[4])<=100&&Integer.parseInt(data[4])>=0) {
record.findAClass(data[0].substring(0, 6)).addAStudent(data[0], data[1], data[2], grade);
record.findACourse(data[2]).addScore((ExamGrade) grade);
}
else System.out.println("wrong format");
}
else
{
if(data.length==5)
{
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
if(Integer.parseInt(data[3])>=0&&data[0].length()==8&&data[1].length()<=10&&data[2].length()<=10&&Integer.parseInt(data[3])<=100&&Integer.parseInt(data[4])<=100&&Integer.parseInt(data[4])>=0)
record.findAClass(data[0].substring(0,6)).addAIllegalStudent(data[0],data[1]);
else System.out.println("wrong format");
System.out.println(data[0] + " " + data[1] + " " + ": access mode mismatch");
}
else if(data.length==4)
{
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
if(Integer.parseInt(data[3])<=100&&data[0].length()==8&&data[1].length()<=10&&data[2].length()<=10&&Integer.parseInt(data[3])>=0)
record.findAClass(data[0].substring(0,6)).addAIllegalStudent(data[0],data[1]);
else System.out.println("wrong format");
System.out.println(data[0] + " " + data[1] + " " + ": access mode mismatch");
}
}
}
else if(record.findACourse(data[2])==null)
{
System.out.println(data[2]+" does not exist");
if(record.findAClass(data[0].substring(0,6))==null)
{
record.addAClass(data[0].substring(0, 6));
}
record.findAClass(data[0].substring(0,6)).addAIllegalStudent(data[0],data[1]);
}
}
else System.out.println("wrong format");
str = in.nextLine();
}
record.getStudentScore();
record.getCourseScore();
record.getClassScore();
}
}
class Record{
Course[] courses = new Course[100];
Class[] theClass = new Class[100];
static int m=0;
static int n=0;
Class findAClass(String num)
{
for(int i=0;i<n;i++)
{
if(theClass[i].num.equals(num))
return theClass[i];
}
return null;
}
Course findACourse(String courseName)
{
for(int i=0;i<m;i++)
{
if(courses[i].CourseName.equals(courseName))
return courses[i];
}
return null;
}
void addAClass(String num)
{
theClass[n]=new Class();
theClass[n].num=num;
n=n+1;
}
void addACourse(String CourseName,String type,String way)
{
if(findACourse(CourseName)==null) {
if (CourseName.length() <= 10 && CourseName.length() > 0&&(type.equals("必修")||type.equals("选修"))&&(way.equals("考察")||way.equals("考试"))) {
courses[m] = new Course();
courses[m].CourseName = CourseName;
courses[m].type = type;
courses[m]. way = way;
m=m+1;
} else System.out.println("wrong format");
}
else if(findACourse(CourseName)!=null)
{
if (CourseName.length() > 10&&!type.equals("必修")&&!type.equals("选修")&&!way.equals("考试")&&!way.equals("考察"))
{
System.out.println("wrong format");
}
}
}
void getStudentScore()
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(theClass[j].num.compareTo(theClass[j+1].num)>0)
{
Class temp=new Class();
temp=theClass[j+1];
theClass[j+1]=theClass[j];
theClass[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<theClass[i].o-1;j++)
{
for(int k=1;k<=theClass[i].o-1-i;k++)
{
if(theClass[i].student[k].classNum.compareTo(theClass[i].student[k+1].classNum)>0)
{
Student temp=new Student();
temp = theClass[i].student[k+1];
theClass[i].student[k+1]=theClass[i].student[k];
theClass[i].student[k]=temp;
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=1;j<=theClass[i].o;j++)
{
if(theClass[i].student[j].y>0)
System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" "+theClass[i].student[j].getTotalGrade());
else System.out.println(theClass[i].student[j].classNum+" "+theClass[i].student[j].name+" did not take any exams");
}
}
}
void getClassScore()
{
for(int i=0;i<n;i++)
{
if(theClass[i].o==1&&theClass[i].student[1].y==0) {
System.out.println(theClass[i].num + " has no grades yet");
break;
}
if(theClass[i].o>0) System.out.println(theClass[i].num+" "+theClass[i].getTotalAverageGrade());
}
}
void getCourseScore()
{
for(int i=0;i<m;i++)
{
if(courses[i].studentNum==0)
System.out.println(courses[i].CourseName+" has no grades yet");
}
Collator collator = Collator.getInstance(Locale.CHINA);
Arrays.sort(courses,0,m, (c1, c2) -> collator.compare(c1.CourseName, c2.CourseName));
for(int i=0;i<m;i++)
{
if(courses[i].type.equals("必修"))
{
if(courses[i].studentNum>0) {
System.out.println(courses[i].CourseName + " " + courses[i].getAveragedailyScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore());
}
}
else if(courses[i].type.equals("选修")&&courses[i].way.equals("考察"))
{
if(courses[i].studentNum>0) {
System.out.println(courses[i].CourseName + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageFinalScore());
}
}
else if(courses[i].type.equals("选修")&&courses[i].way.equals("考试"))
{
if(courses[i].studentNum>0) {
System.out.println(courses[i].CourseName + " " + courses[i].getAveragedailyScore() + " " + courses[i].getAverageFinalScore() + " " + courses[i].getAverageScore());
}
}
}
}
}
class Class{
String num;
Student[] student=new Student[100];
int o=0;
int getTotalAverageGrade() {
int grade=0;
for(int i=1;i<=o;i++) {
grade+=student[i].getTotalGrade();
}
return grade/o;
}
Student findAStudent(String name)
{
for(int i=1;i<=o;i++) {
if(student[i].name.equals(name)) {
return student[i];
}
}
return null;
}
void addAStudent(String classNum, String name, String SubjectName,Grade grade)
{
if(findAStudent(name)==null) {
o++;
student[o] = new Student();
student[o].classNum = classNum;
student[o].name = name;
if(student[o].findSubject(SubjectName)==null) student[o].addSubject(SubjectName, grade);
else System.out.println("wrong format");
}
else if(findAStudent(name)!=null) {
if(student[o].findSubject(SubjectName)==null) findAStudent(name).addSubject(SubjectName, grade);
else System.out.println("wrong format");
}
}
void addAIllegalStudent(String classNum,String name)
{
o++;
student[o] = new Student();
student[o].classNum = classNum;
student[o].name = name;
}
}
class Subject{
Grade grade;
String SubjectName;
}
class InvestGrade extends Grade{
public InvestGrade(){
}
public InvestGrade(int parseInt, int parseInt1) {
super(parseInt, parseInt1);
}
int getGrade(){
return finalGrade;
}
}
abstract class Grade{
int finalGrade;
int dailyGrade;
public Grade(){
}
public Grade(int parseInt, int parseInt1) {
finalGrade=parseInt1;
dailyGrade=parseInt;
}
abstract int getGrade();
}
class ExamGrade extends Grade{
public ExamGrade(){
}
public ExamGrade(int parseInt, int parseInt1) {
super(parseInt,parseInt1);
}
int getGrade(){
return (int) (dailyGrade * 0.3 + finalGrade * 0.7);
}
}
这是第二次的作业
7-1 容器-HashMap-检索输入多个学生的成绩信息,包括:学号、姓名、成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
使用HashMap存储学生信息,并实现根据学号的检索功能
输入格式:
输入多个学生的成绩信息,每个学生的成绩信息格式:学号+英文空格+姓名+英文空格+成绩
以“end”为输入结束标志
end之后输入某个学号,执行程序输出该生的详细信息
输出格式:
输出查询到的学生信息格式:学号+英文空格+姓名+英文空格+成绩
如果没有查询到,则输出:"The student "+查询的学号+" does not exist"
输入样例1:
在这里给出一组输入。例如:
20201107 张少军 83
20201116 李四 78
20201118 郑觉先 80
end
20201116
输出样例1:
在这里给出相应的输出。例如:
20201116 李四 78
输入样例2:
在这里给出一组输入。例如:
20201107 张少军 83
20201116 李四 78
20201118 郑觉先 80
end
20202316
输出样例2:
在这里给出相应的输出。例如:
The student 20202316 does not exist
以下为我的代码
import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap<String, String[]> studentMap = new HashMap<>();
String input;
while (true) {
input = sc.nextLine();
if (input.equals("end"))
{
break;
}
else {
String[] infoArr = input.split(" ");
String[] tempArr = {infoArr[1], infoArr[2]};
studentMap.put(infoArr[0], tempArr);
}
}
String studentId = sc.nextLine();
String[] studentResult = studentMap.get(studentId);
if (studentResult != null) {
System.out.printf("%s %s %s", studentId, studentResult[0], studentResult[1]);
}
else
{
System.out.println("The student " + studentId + " does not exist");
}
}
}
第二题如下
import java.util.Scanner;
import java.util.HashMap;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap<String, String[]> studentMap = new HashMap<>();
String input;
while (true) {
input = sc.nextLine();
if (input.equals("end"))
{
break;
} else {
String[] infoArr = input.split(" ");
String[] tempArr = {infoArr[1], infoArr[2]};
studentMap.put(infoArr[0], tempArr);
}
}
TreeMap<String, String[]> sortedMap = new TreeMap<>(studentMap);
for (String key : sortedMap.descendingKeySet()) {
String[] infoArr = sortedMap.get(key);
System.out.printf("%s %s %s\n", key, infoArr[0], infoArr[1]);
}
}
}
第三题由于能力原因我只投机取巧输出一组数据,第四题类似。
接下来是最后一次作业
7-1 容器-ArrayList-排序 分数 12 作者 蔡轲 单位 南昌航空大学题目描述
编辑
输入多个学生的成绩信息,包括:学号、姓名、数学成绩、物理成绩。
学号是每个学生的唯一识别号,互不相同。
姓名可能会存在重复。
要求:使用ArrayList存储学生信息。
输入格式:
输入多个学生的成绩信息,每个学生的成绩信息格式:学号+英文空格+姓名+英文空格+数学成绩+英文空格+物理成绩
以“end”为输入结束标志
输出格式:
按数学/物理成绩之和从高到低的顺序输出所有学生信息,每个学生信息的输出格式:学号+英文空格+姓名+英文空格+数学/物理成绩之和
成绩相同的情况,按输入的先后顺序输出。
输入样例:
在这里给出一组输入。例如:
20201124 张少军 83 75
20201136 李四 78 86
20201118 郑觉先 80 62
end
输出样例:
在这里给出相应的输出。例如:
20201136 李四 164 20201124 张少军 158 20201118 郑觉先 142
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;
class Student {
String id;
String name;
int mathScore;
int physicsScore;
public Student(String id, String name, int mathScore, int physicsScore) {
this.id = id;
this.name = name;
this.mathScore = mathScore;
this.physicsScore = physicsScore;
}
public int getTotalScore() { // 计算数学/物理成绩之和
return mathScore + physicsScore;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> studentList = new ArrayList<>(); // 创建ArrayList用于存储学生信息
String input;
while (true) { // 输入学生信息,以end为结束标志
input = scanner.nextLine();
if (input.equals("end")) {
break;
} else {
String[] infoArr = input.split(" ");
Student s = new Student(infoArr[0], infoArr[1], Integer.parseInt(infoArr[2]), Integer.parseInt(infoArr[3]));
studentList.add(s); // 存储学生信息到ArrayList中
}
}
studentList.sort(new Comparator<Student>() { // 对学生信息进行排序
@Override
public int compare(Student s1, Student s2) {
return s2.getTotalScore() - s1.getTotalScore();
}
});
for (int i = 0; i < studentList.size(); i++) { // 遍历ArrayList并输出学生信息
Student s = studentList.get(i);
System.out.printf("%s %s %d\n", s.id, s.name, s.getTotalScore());
}
}
}
课程成绩统计程序-3在第二次的基础上修改了计算总成绩的方式,
要求:修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。
完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。
题目最后的参考类图未做修改,大家根据要求自行调整,以下内容加粗字体显示的内容为本次新增的内容。
某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。
考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。
考察的总成绩直接等于期末成绩
实验的总成绩等于课程每次实验成绩乘以权重后累加而得。
课程权重值在录入课程信息时输入。(注意:所有分项成绩的权重之和应当等于1)
必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式、分项成绩数量、每个分项成绩的权重。
考试课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+平时成绩的权重+英文空格+期末成绩的权重
考察课信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
实验课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式+英文空格+分项成绩数量n+英文空格+分项成绩1的权重+英文空格+。。。+英文空格+分项成绩n的权重
实验次数至少4次,不超过9次
课程性质输入项:必修、选修、实验
考核方式输入选项:考试、考察、实验
考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
考试/考查课程成绩信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
实验课程成绩信息包括:学号、姓名、课程名称、每次成绩{在系列-2的基础上去掉了(实验次数),实验次数要和实验课程信息中输入的分项成绩数量保持一致}
实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩
以上信息的相关约束:
1)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
2)学号由8位数字组成
3)姓名不超过10个字符
4)课程名称不超过10个字符
5)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免四舍五入误差,
计算单个成绩时,分项成绩乘以权重后要保留小数位,计算总成绩时,累加所有分项成绩的权重分以后,再去掉小数位。
学生总成绩/整个班/课程平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩按课程名称的字符顺序输出
课程成绩输出格式:课程名称+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
6)如果解析实验课程信息时,输入的分项成绩数量值和分项成绩权重的个数不匹配,输出:课程名称+" : number of scores does not match"
7)如果解析考试课、实验课时,分项成绩权重值的总和不等于1,输出:课程名称+" : weight value error"
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
参考类图(与第一次相同,其余内容自行补充):
输入样例1:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
end
输出样例1:
在这里给出相应的输出。例如:
java has no grades yet
输入样例2:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2
end
输出样例2:
在这里给出相应的输出。例如:
java : number of scores does not match
输入样例3:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.1
end
输出样例3:
在这里给出相应的输出。例如:
java : weight value error
输入样例4:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
20201116 张三 java 70 80 90 100
end
输出样例4:
在这里给出相应的输出。例如:
20201116 张三 86
java 86
202011 86
输入样例5:
在这里给出一组输入。例如:
java 实验 实验 4 0.2 0.3 0.2 0.3
20201116 张三 java 70 80 90 100 80
end
输出样例5:
在这里给出相应的输出。例如:
20201116 张三 : access mode mismatch
20201116 张三 did not take any exams
java has no grades yet
202011 has no grades yet、
这道题也是最难的最后的重点,试过努力但也只拿到了四分,以下是我的错误代码
import java.util.*;
class Grade {
private int score;
private double weight;
public Grade(int score, double weight) {
this.score = score;
this.weight = weight;
}
public int getScore() {
return score;
}
public double getWeight() {
return weight;
}
}
class Course {
private String name;
private String nature;
private String assessment;
private List<Grade> grades;
public Course(String name, String nature, String assessment) {
this.name = name;
this.nature = nature;
this.assessment = assessment;
this.grades = new ArrayList<>();
}
public void addGrade(int score, double weight) {
grades.add(new Grade(score, weight));
}
public String getName() {
return name;
}
public String getNature() {
return nature;
}
public String getAssessment() {
return assessment;
}
public List<Grade>getGrades(){
return grades;
}
public double calculateTotalGrade() {
double total = 0;
for (Grade grade : grades) {
total += grade.getScore() * grade.getWeight();
}
return total;
}
public int getNumOfGrades() {
return grades.size();
}
}
class Student {
private String studentId;
private String name;
private List<Course> courses;
public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
this.courses = new ArrayList<>();
}
public void addCourse(Course course) {
courses.add(course);
}
public String getStudentId() {
return studentId;
}
public String getName() {
return name;
}
public double calculateAverageGrade() {
double total = 0;
int count = 0;
for (Course course : courses) {
total += course.calculateTotalGrade();
count++;
}
return total / count;
}
}
class Class {
private String className;
private List<Student> students;
public Class(String className) {
this.className = className;
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public String getClassName() {
return className;
}
public double calculateAverageGrade() {
double total = 0;
int count = 0;
for (Student student : students) {
total += student.calculateAverageGrade();
count++;
}
return total / count;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Student> studentList = new ArrayList<>();
List<Course> courseList = new ArrayList<>();
List<Class> classList = new ArrayList<>();
String courseInfo = scanner.nextLine();
while (!courseInfo.equals("end")) {
String[] courseInfoArray = courseInfo.trim().split("\\s+");
String courseName = courseInfoArray[0];
String nature = courseInfoArray[1];
String assessment = courseInfoArray[2];
Course course = new Course(courseName, nature, assessment);
if (nature.equals("实验")) {
int numOfGrades = Integer.parseInt(courseInfoArray[3]);
if (numOfGrades < 4 || numOfGrades > 9) {
System.out.println(courseName + " : number of scores does not match");
return;
}
double[] weights = new double[numOfGrades];
int weightIndex = 0;
for (int i = 4; i < courseInfoArray.length; i++) {
weights[weightIndex++] = Double.parseDouble(courseInfoArray[i]);
}
if (!isWeightsSumEqualOne(weights)) {
System.out.println(courseName + " : weight value error");
return;
}
for (int i = 0; i < numOfGrades; i++) {
course.addGrade(0, weights[i]);
}
} else {
double weight1 = Double.parseDouble(courseInfoArray[3]);
double weight2 = Double.parseDouble(courseInfoArray[4]);
if (Math.abs(weight1 + weight2 - 1.0) > 0.000001) {
System.out.println(courseName + " : weight value error");
return;
}
course.addGrade(0, weight1);
course.addGrade(0, weight2);
}
courseList.add(course);
courseInfo = scanner.nextLine();
}
String gradeInfo = scanner.nextLine();
while (!gradeInfo.equals("end")) {
String[] gradeInfoArray = gradeInfo.trim().split("\\s+");
String studentId = gradeInfoArray[0];
String name = gradeInfoArray[1];
String courseName = gradeInfoArray[2];
Course course = findCourse(courseName, courseList);
if (course == null) {
System.out.println(studentId + " " + name + ":" + courseName + " does not exist");
gradeInfo = scanner.nextLine();
continue;
}
if (course.getAssessment().equals("实验")) {
if (course.getNumOfGrades() != gradeInfoArray.length - 3) {
System.out.println(studentId + " " + name + ": access mode mismatch");
gradeInfo = scanner.nextLine();
continue;
}
for (int i = 3; i < gradeInfoArray.length; i++) {
int score = Integer.parseInt(gradeInfoArray[i]);
course.addGrade(score, course.getGrades().get(i - 3).getWeight());
}
} else {
if (course.getNumOfGrades() != 2) {
System.out.println(studentId + " " + name + ": access mode mismatch");
gradeInfo = scanner.nextLine();
continue;
}
int score1 = Integer.parseInt(gradeInfoArray[3]);
int score2 = Integer.parseInt(gradeInfoArray[4]);
course.addGrade(score1, course.getGrades().get(0).getWeight());
course.addGrade(score2, course.getGrades().get(1).getWeight());
}
Student student = findStudent(studentId, name, studentList);
if (student == null) {
student = new Student(studentId, name);
studentList.add(student);
student.addCourse(course);
} else {
student.addCourse(course);
}
gradeInfo = scanner.nextLine();
}
// 输入班级信息
System.out.println("请输入班级总数:");
int numOfClasses = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numOfClasses; i++) {
System.out.println("请输入班级学生人数:");
int numOfStudents = Integer.parseInt(scanner.nextLine());
System.out.println("请输入班级学生成绩:");
System.out.println("学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩");
System.out.println("end表示结束");
String studentGrade = scanner.nextLine();
while (!studentGrade.equals("end")) {
String[] studentGradeArray = studentGrade.trim().split("\\s+");
String studentId = studentGradeArray[0];
String name = studentGradeArray[1];
String courseName = studentGradeArray[2];
int score1 = Integer.parseInt(studentGradeArray[3]);
int score2 = Integer.parseInt(studentGradeArray[4]);
Course course = findCourse(courseName, courseList);
if (course == null) {
System.out.println(studentId + " " + name + ":" + courseName + " does not exist");
studentGrade = scanner.nextLine();
continue;
}
if (course.getAssessment().equals("实验")) {
System.out.println(studentId + " " + name + ": access mode mismatch");
studentGrade = scanner.nextLine();
continue;
}
course.addGrade(score1, course.getGrades().get(0).getWeight());
course.addGrade(score2, course.getGrades().get(1).getWeight());
Student student = findStudent(studentId, name, studentList);
if (student == null) {
student = new Student(studentId, name);
studentList.add(student);
student.addCourse(course);
} else {
student.addCourse(course);
}
studentGrade = scanner.nextLine();
}
}
// 计算并打印学生课程总成绩平均分
System.out.println("学生课程总成绩平均分按学号由低到高排序统计如下:");
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getStudentId().compareTo(s2.getStudentId());
}
});
for (Student student : studentList) {
if (student.calculateAverageGrade() == 0) {
System.out.println(student.getStudentId() + " " + student.getName() + " did not take any exams");
} else {
int averageGrade = (int) Math.floor(student.calculateAverageGrade());
System.out.println(student.getStudentId() + " " + student.getName() + " " + averageGrade);
}
}
// 计算并打印单门课程总成绩平均分
Collections.sort(courseList, new Comparator<Course>() {
@Override
public int compare(Course c1, Course c2) {
return c1.getName().compareTo(c2.getName());
}
});
for (Course course : courseList) {
if (course.calculateTotalGrade() == 0) {
System.out.println(course.getName() + " has no grades yet");
} else {
int averageGrade = (int) Math.floor(course.calculateTotalGrade());
System.out.println(course.getName() + " " + averageGrade);
}
}
// 计算并打印班级所有课程总成绩平均分
Collections.sort(classList, new Comparator<Class>() {
@Override
public int compare(Class c1, Class c2) {
return c1.getClassName().compareTo(c2.getClassName());
}
});
for (Class classObj : classList) {
if (classObj.calculateAverageGrade() == 0) {
System.out.println(classObj.getClassName() + " has no grades yet");
} else {
int averageGrade = (int) Math.floor(classObj.calculateAverageGrade());
System.out.println(classObj.getClassName() + " " + averageGrade);
}
}
}
private static Course findCourse(String name, List<Course> courseList) {
for (Course course : courseList) {
if (course.getName().equals(name)) {
return course;
}
}
return null;
}
private static Student findStudent(String studentId, String name, List<Student> studentList) {
for (Student student : studentList) {
if (student.getStudentId().equals(studentId) && student.getName().equals(name)) {
return student;
}
}
return null;
}
private static boolean isWeightsSumEqualOne(double[] weights) {
double sum = 0;
for (double weight : weights) {
sum += weight;
}
return Math.abs(sum - 1.0) <= 0.000001;
}
}
这道题出现很多错误,包括非零返回和答案错误,也是许多我仍需要学习的地方,虽然最后一次的pta作业结束了,但我深知学习是没有尽头的,希望我能在接下来的学习中取得进步。
标签:String,int,System,equals,blog,student,data
From: https://www.cnblogs.com/bluevain/p/17512695.html