(一)前言:
该一阶段作业主要围绕 课程成绩统计程序这一主题完成的,中间考察了Arraylist,Map,Set,正则表达式等等,这一阶段题目量不算太多,但是题目难度确实挺高,但是后面的作业能够再前面的代码基础上改,这就大大减少了我们的代码量,但是前面的思路要对,要满足可拓展性,这样后面才能用。
(二)设计与分析
题目集(八)课程成绩统计程序-1

import java.text.Collator;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
Collator co=Collator.getInstance(Locale.CHINA);
School sc =new School();
Courseselectionsystem s =new Courseselectionsystem();
while (!a.equals("end")) {
Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)(必修|选修)\\s(?!\\s)(考试|考察)$");
Matcher m = pattern.matcher(a);
if (m.matches()) {
String[] number = a.split("\\s");
if (number[1].equals("必修") && number[2].equals("考察")) {
System.out.println(number[0] + " : course type & access mode mismatch");
} else {
if(s.searthCourse(number[0])==null)
s.addCourse(number[0], number[1], number[2]);
}
} else {
Pattern pattern1 = Pattern.compile("^[0-9]{8}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}(\\s(?!\\s)(\\d{1,2}|100)){1,2}$");
m = pattern1.matcher(a);
if (m.matches()) {
String[] number = a.split("\\s");
if (s.searthCourse(number[2]) != null) {
int length = number.length;
if (length == 4) {
if (s.searthCourse(number[2]).assessmentmethod.equals("考试")) {
System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if(sc.searthclass(Integer.parseInt(number[0])/100)==null)
sc.addclass(Integer.parseInt(number[0])/100);
s.addStudent2(number[0],number[1]);
} else {
if(sc.searthclass(Integer.parseInt(number[0])/100)!=null)
{if(sc.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]))==null)
sc.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2],number[3]);
else if(sc.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null)
sc.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));
}
else {sc.addclass(Integer.parseInt(number[0])/100);
sc.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2],number[3]);}
if(s.searthStudent(number[1],Integer.parseInt(number[0]))==null)
{s.addStudent(number[0], number[1], number[2], number[3]);
s.searthStudent(number[1],Integer.parseInt(number[0])).n++;
s.searthCourse(number[2]).setScores(new Examinegrades(Integer.parseInt(number[3])));
s.searthCourse(number[2]).n++;}
else if(s.searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null) {s.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));
s.searthStudent(number[1], Integer.parseInt(number[0])).n++;
s.searthCourse(number[2]).setScores(new Examinegrades(Integer.parseInt(number[3])));
s.searthCourse(number[2]).n++;}
}
} else if (length == 5) {
if (s.searthCourse(number[2]).assessmentmethod.equals("考察")) {
System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (sc.searthclass(Integer.parseInt(number[0]) / 100) == null)
sc.addclass(Integer.parseInt(number[0]) / 100);
s.addStudent2(number[0], number[1]);
}
else{
if (sc.searthclass(Integer.parseInt(number[0]) / 100) != null) {
if (sc.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])) == null)
sc.searthclass(Integer.parseInt(number[0]) / 100).addStudent1(number[0], number[1], number[2], number[3], number[4]);
else if (sc.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null)
sc.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
} else {
sc.addclass(Integer.parseInt(number[0]) / 100);
sc.searthclass(Integer.parseInt(number[0]) / 100).addStudent1(number[0], number[1], number[2], number[3], number[4]);
}
if (s.searthStudent(number[1], Integer.parseInt(number[0])) == null) {
s.addStudent1(number[0], number[1], number[2], number[3], number[4]);
s.searthStudent(number[1], Integer.parseInt(number[0])).n++;
} else if (s.searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null)
{ s.searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
s.searthStudent(number[1], Integer.parseInt(number[0])).n++; }
s.searthCourse(number[2]).setScores(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
s.searthCourse(number[2]).n++;
}
}
} else {
System.out.println(number[2] + " does not exist");
if(sc.searthclass(Integer.parseInt(number[0])/100)==null)
sc.addclass(Integer.parseInt(number[0])/100);
s.addStudent2(number[0],number[1]);
}
} else {
System.out.println("wrong format");
}
}
a = input.nextLine();
} //学生学号排序
for(int i=0;i < s.s-1;i++)
{
for(int j = 0;j < s.s - i-1;j++)
{
if(s.getStudent(j).getId()> s.getStudent(j+1).getId())
{
Student student1 = s.getStudent(j);
Student student2 =s.getStudent(j+1);
s.setStudent(student1,j+1);
s.setStudent(student2,j);
}
}
}
for(int i1=0;i1< s.s;i1++)
{
if(s.getStudent(i1).n!=0)
{
if(s.getStudent(i1).usualgrades()!=0)
System.out.println(s.getStudent(i1).getId()+" "+s.getStudent(i1).getName()+" "+s.getStudent(i1).getaverage());
else System.out.println(s.getStudent(i1).getId()+" "+s.getStudent(i1).getName()+" "+s.getStudent(i1).getaverage());
}
else System.out.println(s.getStudent(i1).getId()+" "+s.getStudent(i1).getName()+" did not take any exams");
} //课程名称的字符顺序排序
for(int i=0;i < s.c-1;i++)
{
for(int j = 0;j < s.c - i-1;j++)
{
if(co.compare(s.getCourse(j).name,s.getCourse(j+1).name)>0 )
{
Course course1 = s.getCourse(j);
Course course2 =s.getCourse(j+1);
s.setCourse(course1,j+1);
s.setCourse(course2,j);
}
}
}
for(int i1=0;i1< s.c;i1++)
{
if(s.getCourse(i1).n!=0)
{
if(s.getCourse(i1).usualgrades()!=0)
System.out.println(s.getCourse(i1).name+" "+s.getCourse(i1).usualgrades()+" "+s.getCourse(i1).finalgrades()+" "+s.getCourse(i1).getaverage());
else System.out.println(s.getCourse(i1).name+" "+s.getCourse(i1).finalgrades()+" "+s.getCourse(i1).getaverage());
}
else System.out.println(s.getCourse(i1).name+" has no grades yet");
}//按照班级平均分排序
for(int i=0;i < sc.k-1;i++)
{
for(int j = 0;j < sc.k - i-1;j++)
{
if(sc.getClass(j).average()<=sc.getClass(j+1).average())
{
Class class2 = sc.getClass(j);
Class class3 = sc.getClass(j+1);
sc.setClass1(class2,j+1);
sc.setClass1(class3,j);
}
}
}
for(int i=0;i< sc.k;i++)
{
if(sc.getClass(i).k!=0)
System.out.println(sc.getClass(i).getNumber()+" "+sc.getClass(i).average());
else System.out.println(sc.getClass(i).getNumber()+" has no grades yet");
}
}
}
class Class {
private int nu;//学号
private Student[] st =new Student[100];//学生
int k = 0;//学生
public int getNumber() {
return nu;
}
public void setNumber(int nu) {
this.nu = nu;
}
public void addStudent(String xuehao,String name,String course,String grade) {
st[k] = new Student();
st[k].setName(name);
st[k].setId(Integer.parseInt(xuehao));
st[k].setClass1(course);
st[k].setScore(new Examinegrades(Integer.parseInt(grade)));
k++;
}
public void addStudent1(String xuehao,String name,String course,String grade1,String grade2) {
st[k] = new Student();
st[k].setName(name);
st[k].setId(Integer.parseInt(xuehao));
st[k].setClass1(course);
st[k].setScore(new Examscores(Integer.parseInt(grade1),Integer.parseInt(grade2)));
k++;
}
public int average()
{int al =0;
for(int i =0;i<k;i++)
{
al = al + this.st[i].getaverage();
}
return al/k;
}
public Student searthStudent(String dishName,int id)
{
int s1 = 0;
while(s1<k) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
}
class Course {
String name;
String style;
String assessmentmethod;
Score[] sc =new Score[100] ;
int k =0;
int n = 0;
public int usualgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].usualgrades;
return all/k;
}
public int getaverage() {
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].average;
return all/k;
}
public int finalgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].finalgrades ;
return all/k;
}
public void setScores(Score scores) {
this.sc[k] = scores;
k=k+1;
}
}
class Courseselectionsystem {
Course[] co =new Course[100];
Student[] st =new Student[100];
int c = 0;
int s = 0;
public void addCourse(String name,String style,String assessmentmethod)
{
co[c] = new Course();
co[c].name=name;
co[c].style=style;
co[c].assessmentmethod=assessmentmethod;
c=c+1;
}
public Course searthCourse(String dishName)//
{
int c1 = 0;
while(c1<c) {
if(co[c1].name.equals(dishName) )
return co[c1];
c1 = c1 + 1;
}
return null;
}
public void addStudent2(String xuehao,String name) {
st[s] = new Student();
st[s].setName(name);
st[s].setId(Integer.parseInt(xuehao));
s++;
}
public void addStudent(String xuehao,String name,String course,String grade) {
st[s] = new Student();
st[s].setName(name);
st[s].setClass1(course);
st[s].setId(Integer.parseInt(xuehao));
st[s].setScore(new Examinegrades(Integer.parseInt(grade)));
s++;
}
public void addStudent1(String xuehao,String name,String course,String grade1,String grade2) {
st[s] = new Student();
st[s].setName(name);
st[s].setClass1(course);
st[s].setId(Integer.parseInt(xuehao));
st[s].setScore(new Examscores(Integer.parseInt(grade1),Integer.parseInt(grade2)));
s++;
}
public Student searthStudent(String dishName,int id)//
{
int s1 = 0;
while(s1<s) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
public void setStudent(Student student,int a) {
this.st[a] = student;
}
public Course getCourse(int a) {
return co[a];
}
public void setCourse(Course course,int a) {
this.co[a] = course;
}
public Student getStudent(int a) {
return st[a];
}
}
class Examinegrades extends Score{
public Examinegrades(int finalgrades) {
this.finalgrades = finalgrades;
this.average = finalgrades;
}
}
class Examscores extends Score{
public Examscores(int usualgrades, int finalgrades) {
this.usualgrades = usualgrades;
this.finalgrades = finalgrades;
this.average = this.average+(int) Math.floor(usualgrades*0.3+finalgrades*0.7);
}
}
class School {
private Class[] cl= new Class[100];
int k = 0;
public void addclass(int a)
{
cl[k] = new Class();
cl[k].setNumber(a);
k=k+1;
}
public Class searthclass(int a)
{
int k1 = 0;
while(k1<k) {
if(cl[k1].getNumber()==a )
return cl[k1];
k1 = k1 + 1;
}
return null;
}
public Class getClass(int a) {
return cl[a];
}
public void setClass1(Class class1,int a) {
this.cl[a] = class1;
}
}
abstract class Score {
protected int average = 0;
protected int usualgrades=0;
protected int finalgrades=0 ;
}
class Student {
private String name;
private int id;
int n=0;
int k=0;
int c =0;
private Score[] sc =new Score[100] ;
private String[] cl =new String[100];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public int usualgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].usualgrades;
return all/k;
}
public int getaverage() {
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].average;
if(all==0) return 0;
else return all/k;
}
public void setScore(Score score) {
sc[k] = score;
k ++;
}
public String getClass1(int a) {
return cl[a];
}
public void setClass1(String class1) {
cl[c] = class1;
c++;
}
public String search(String a)
{
for(int i=0;i<c;i++)
{
if(getClass1(i).equals(a))
{
return getClass1(i);}
}
return null;
}
public void setId(int id) {
this.id = id;
}
}
-
类
Student:表示学生,包含学生的姓名、学号、总分和所属班级。 -
抽象类 Score:表示成绩,包含期中成绩和期末成绩和总评。
-
子类
Examscore:表示考试成绩,包含两个成绩部分:期中成绩和期末成绩。具有计算考试成绩的方法。 -
子类 Examinegrades:表示考察成绩,只包含一个成绩部分:期末成绩。
-
类
Course:表示课程名字,考核方式,分数等。 -
类Courseselectionsystem:包含学生变量和课程变量。
-
类
Class:表示班级,包含班号、学生列表和班级平均分 - 类School:表示该学校有哪些班级。
-
类
Main:包含主函数,用于执行学生成绩管理系统的逻辑。其中包含了对输入的解析、创建对象、计算成绩等操作。 - 设计分析:
- 学生有自己的姓名学号和分数,课程也有自己的姓名,考核方式,分数,班级有学生,有分数,班号,然后学校里面有班级。
- 踩坑心得:
刚开始使用compareTo来进行排序,结过题目是根据拼音排序,导致一开始很多点没过,后面使用Collator co=Collator.getInstance(Locale.CHINA);这个方法解决了。
- 建议:使用正则表达来匹配成绩,不能根据字符串长度来,不然不满足可拓展性,后面就可能出现问题。
- 7-3 课程成绩统计程序-2
- 类图:
-

源码:
// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`,
// 然后按 Enter 键。现在,您可以在代码中看到空格字符。
import java.text.Collator;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
Collator co=Collator.getInstance(Locale.CHINA);
School school =new School();
Courseselectionsystem select =new Courseselectionsystem();
while (!a.equals("end")) {
Pattern pattern = Pattern.compile("^[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)(必修|选修|实验)\\s(?!\\s)(考试|考察|实验)$");
Matcher m = pattern.matcher(a);
if (m.matches()) {
String[] number = a.split("\\s+");
if ((number[1].equals("必修") && (number[2].equals("考察")|number[2].equals("实验")))|(number[1].equals("选修")&&number[2].equals("实验"))|(number[1].equals("实验")&&(number[2].equals("考试")|(number[2].equals("考核"))))) {
if(select.searthCourse(number[0])==null)
System.out.println(number[0] + " : course type & access mode mismatch");
} else {
if(select.searthCourse(number[0])==null)
select.addCourse(number[0], number[1], number[2]);
}
} else {
Pattern pattern1 = Pattern.compile("^[0-9]{8}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}((\\s(?!\\s)((\\d{1,2}|100))){1,2}|(\\s(?!\\s)[4-9](\\s(?!\\s)((\\d{1,2}|100))){1,9}))$");
m = pattern1.matcher(a);
if (m.matches()) {
String[] number = a.split("\\s+");
if (select.searthCourse(number[2]) != null) {
int length = number.length;
if (length == 4) {
if (select.searthCourse(number[2]).assessmentmethod.equals("考试")|select.searthCourse(number[2]).assessmentmethod.equals("实验")) {
System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if(school.searthclass(Integer.parseInt(number[0])/100)==null)
school.addclass(Integer.parseInt(number[0])/100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0],number[1]);} else {
if(school.searthclass(Integer.parseInt(number[0])/100)!=null)
{if(school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]))==null)
{school.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));}
else if(school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null) {
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]) ).setClass1(number[2]);
school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));
}}
else {school.addclass(Integer.parseInt(number[0])/100);
school.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));
}
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
{
select.addStudent(number[0], number[1], number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0]) ).setScore(new Examinegrades(Integer.parseInt(number[3])));
select.searthStudent(number[1],Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(new Examinegrades(Integer.parseInt(number[3])));
select.searthCourse(number[2]).n++;
select.searthCourse(number[2]).setScores(new Examinegrades(Integer.parseInt(number[3])));
select.searthCourse(number[2]).n++;}
else if(select.searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null) {
select.searthCourse(number[2]).setScores(new Examinegrades(Integer.parseInt(number[3])));
select.searthCourse(number[2]).n++;
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examinegrades(Integer.parseInt(number[3])));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
}}
} else if (length == 5) {
if (select.searthCourse(number[2]).assessmentmethod.equals("考察")|select.searthCourse(number[2]).assessmentmethod.equals("实验")) {System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (school.searthclass(Integer.parseInt(number[0]) / 100) == null)
school.addclass(Integer.parseInt(number[0]) / 100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0], number[1]);}
else{
if (school.searthclass(Integer.parseInt(number[0]) / 100) != null) {
if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])) == null) {
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]),Integer.parseInt(number[4]))); }
else if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null) {
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]) ).setClass1(number[2]);
school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
}
} else {
school.addclass(Integer.parseInt(number[0]) / 100);
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]),Integer.parseInt(number[4])));
}
if (select.searthStudent(number[1], Integer.parseInt(number[0])) == null) {
select.addStudent(number[0], number[1], number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]),Integer.parseInt(number[4])));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
select.searthCourse(number[2]).n++;
} else if (select.searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null)
{ select.searthCourse(number[2]).setScores(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
select.searthCourse(number[2]).n++;
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);
select.searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Examscores(Integer.parseInt(number[3]), Integer.parseInt(number[4])));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++; }}
}
else if(length>5)
{
if (select.searthCourse(number[2]).assessmentmethod.equals("考察")|select.searthCourse(number[2]).assessmentmethod.equals("考试")) {System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (school.searthclass(Integer.parseInt(number[0]) / 100) == null)
school.addclass(Integer.parseInt(number[0]) / 100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0], number[1]);}
else {
if(Integer.parseInt(number[3])!=length-4)
{
System.out.println(number[0]+" "+number[1]+" : "+"access mode mismatch");
if(school.searthclass(Integer.parseInt(number[0])/100)==null)
school.addclass(Integer.parseInt(number[0])/100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0],number[1]);
}
else {
if (school.searthclass(Integer.parseInt(number[0]) / 100) != null) {
if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])) == null) {
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
setscore(school, number);
} else if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null) {
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]) ).setClass1(number[2]);
setscore(school, number);
}
} else {
school.addclass(Integer.parseInt(number[0]) / 100);
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
setscore(school, number);
}
if (select.searthStudent(number[1], Integer.parseInt(number[0])) == null) {
select.addStudent(number[0], number[1], number[2]);
Selectscore(select, number);
} else if (select.searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null) {
Selectscore(select, number);
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);}
}}
}
} else {
System.out.println(number[2] + " does not exist");
if(school.searthclass(Integer.parseInt(number[0])/100)==null)
school.addclass(Integer.parseInt(number[0])/100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0],number[1]);
}
} else {
System.out.println("wrong format");
}
}
a = input.nextLine();
} //学生学号排序
for(int i=0;i < select.s-1;i++)
{
for(int j = 0;j < select.s - i-1;j++)
{
if(select.getStudent(j).getId()>= select.getStudent(j+1).getId())
{
Student student1 = select.getStudent(j);
Student student2 =select.getStudent(j+1);
select.setStudent(student1,j+1);
select.setStudent(student2,j);
}
}
}
for(int i1=0;i1< select.s;i1++)
{
if(select.getStudent(i1).n!=0)
{
if(select.getStudent(i1).usualgrades()!=0)
System.out.println(select.getStudent(i1).getId()+" "+select.getStudent(i1).getName()+" "+select.getStudent(i1).getaverage());
else System.out.println(select.getStudent(i1).getId()+" "+select.getStudent(i1).getName()+" "+select.getStudent(i1).getaverage());
}
else System.out.println(select.getStudent(i1).getId()+" "+select.getStudent(i1).getName()+" did not take any exams");
} //课程名称的字符顺序排序
for(int i=0;i < select.c-1;i++)
{
for(int j = 0;j < select.c - i-1;j++)
{
if(co.compare(select.getCourse(j).name,select.getCourse(j+1).name)>=0 )
{
Course course1 = select.getCourse(j);
Course course2 =select.getCourse(j+1);
select.setCourse(course1,j+1);
select.setCourse(course2,j);
}
}
}
for(int i1=0;i1< select.c;i1++)
{
if(select.getCourse(i1).n!=0)
{
if(select.getCourse(i1).assessmentmethod.equals("实验"))
{
System.out.println(select.getCourse(i1).name+" "+select.getCourse(i1).getaverage());
}
else {if(select.getCourse(i1).usualgrades()!=0)
System.out.println(select.getCourse(i1).name+" "+select.getCourse(i1).usualgrades()+" "+select.getCourse(i1).finalgrades()+" "+select.getCourse(i1).getaverage());
else System.out.println(select.getCourse(i1).name+" "+select.getCourse(i1).finalgrades()+" "+select.getCourse(i1).getaverage());
}
}
else System.out.println(select.getCourse(i1).name+" has no grades yet");
}//按照班级平均分排序
for(int i=0;i < school.k-1;i++)
{
for(int j = 0;j < school.k - i-1;j++)
{
if(school.getClass(j).getSumgrade()/school.getClass(j).getSumcourse()>school.getClass(j+1).getSumgrade()/school.getClass(j+1).getSumcourse())
{Class class2 = school.getClass(j);
Class class3 = school.getClass(j+1);
school.setClass1(class2,j+1);
school.setClass1(class3,j);}
}
}
for(int i=0;i< school.k;i++)
{
if(school.getClass(i).k!=0)
System.out.println(school.getClass(i).getNumber()+" "+school.getClass(i).getSumgrade()/school.getClass(i).getSumcourse());
else System.out.println(school.getClass(i).getNumber()+" has no grades yet");
}
}public static void Selectscore(Courseselectionsystem select, String[] number) {
setscore(select, number);
if (Integer.parseInt(number[3]) == 4)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7])));
else if (Integer.parseInt(number[3]) == 5)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7]), Integer.parseInt(number[8])));
else if (Integer.parseInt(number[3]) == 6)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7]), Integer.parseInt(number[8]), Integer.parseInt(number[9])));
else if (Integer.parseInt(number[3]) == 7)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7]), Integer.parseInt(number[8]), Integer.parseInt(number[9]), Integer.parseInt(number[10])));
else if (Integer.parseInt(number[3]) == 8)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7]), Integer.parseInt(number[8]), Integer.parseInt(number[9]), Integer.parseInt(number[10]), Integer.parseInt(number[11])));
else if (Integer.parseInt(number[3]) == 9)
select.searthCourse(number[2]).setScores(new Experimentgrades(Integer.parseInt(number[4]), Integer.parseInt(number[5]), Integer.parseInt(number[6]), Integer.parseInt(number[7]), Integer.parseInt(number[8]), Integer.parseInt(number[9]), Integer.parseInt(number[10]), Integer.parseInt(number[11]), Integer.parseInt(number[12])));
select.searthCourse(number[2]).n++;
}public static void setscore(Courseselectionsystem select, String[] number) {
if(Integer.parseInt(number[3])==4)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7])));
else if(Integer.parseInt(number[3])==5)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8])));
else if(Integer.parseInt(number[3])==6)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9])));
else if(Integer.parseInt(number[3])==7)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10])));
else if(Integer.parseInt(number[3])==8)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10]),Integer.parseInt(number[11])));
else if(Integer.parseInt(number[3])==9)
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10]),Integer.parseInt(number[11]),Integer.parseInt(number[12])));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
}public static void setscore(School school, String[] number) {
if(Integer.parseInt(number[3])==4)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7])));
else if(Integer.parseInt(number[3])==5)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8])));
else if(Integer.parseInt(number[3])==6)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9])));
else if(Integer.parseInt(number[3])==7)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10])));
else if(Integer.parseInt(number[3])==8)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10]),Integer.parseInt(number[11])));
else if(Integer.parseInt(number[3])==9)
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Experimentgrades(Integer.parseInt(number[4]),Integer.parseInt(number[5]),Integer.parseInt(number[6]),Integer.parseInt(number[7]),Integer.parseInt(number[8]),Integer.parseInt(number[9]),Integer.parseInt(number[10]),Integer.parseInt(number[11]),Integer.parseInt(number[12])));
}
}
class Class {
private int nu;//班号
private Student[] st =new Student[1000];//学生
int k = 0;//学生
public int getNumber() {
return nu;
}public int getSumcourse() {
int sumcourse = 0;
for(int i=0;i<k;i++)
{
sumcourse += st[i].k;
}
return sumcourse;
}
public int getSumgrade()
{
int sumgrade = 0;
for(int i=0;i<k;i++)
{
sumgrade += st[i].getSumgrade();
}
return sumgrade;
}
public void setNumber(int nu) {
this.nu = nu;
}
public void addStudent(String xuehao,String name,String course) {
st[k] = new Student();
st[k].setName(name);
st[k].setId(Integer.parseInt(xuehao));
st[k].setClass1(course);
k++;
}
public int average()
{int al =0;
for(int i =0;i<k;i++)
{
al = al + this.st[i].getaverage();
}
if(k==0) return 0;
return al/k;
}
public Student searthStudent(String dishName,int id)
{
int s1 = 0;
while(s1<k) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
}
class Course {
String name;
String style;
String assessmentmethod;
Score[] sc =new Score[1000] ;
int k =0;
int n = 0;
public int usualgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].usualgrades;
if(k==0) return 0;
return all/k;
}
public int getaverage() {
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].average;
if(k==0) return 0;
return all/k;
}
public int finalgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].finalgrades;
if(k==0) return 0;
return all/k;
}
public void setScores(Score scores) {
this.sc[k] = scores;
k=k+1;
}
}
class Courseselectionsystem {
Course[] co =new Course[1000];
Student[] st =new Student[1000];
int c = 0;
int s = 0;
public void addCourse(String name,String style,String assessmentmethod)
{
co[c] = new Course();
co[c].name=name;
co[c].style=style;
co[c].assessmentmethod=assessmentmethod;
c=c+1;
}
public Course searthCourse(String dishName)//
{
int c1 = 0;
while(c1<c) {
if(co[c1].name.equals(dishName) )
return co[c1];
c1 = c1 + 1;
}
return null;
}
public void addStudent2(String xuehao,String name) {
st[s] = new Student();
st[s].setName(name);
st[s].setId(Integer.parseInt(xuehao));
s++;
}
public void addStudent(String xuehao,String name,String course) {
st[s] = new Student();
st[s].setName(name);
st[s].setClass1(course);
st[s].setId(Integer.parseInt(xuehao));
s++;
}
public Student searthStudent(String dishName,int id)//
{
int s1 = 0;
while(s1<s) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
public void setStudent(Student student,int a) {
this.st[a] = student;
}
public Course getCourse(int a) {
return co[a];
}
public void setCourse(Course course,int a) {
this.co[a] = course;
}
public Student getStudent(int a) {
return st[a];
}
}
class Examinegrades extends Score{
public Examinegrades(int finalgrades) {
this.finalgrades = finalgrades;
this.average = finalgrades;
}
}
class Examscores extends Score{
public Examscores(int usualgrades, int finalgrades) {
this.usualgrades = usualgrades;
this.finalgrades = finalgrades;
this.average = this.average+(int) Math.floor(usualgrades*0.3+finalgrades*0.7);
}
}
class School {
private Class[] cl= new Class[1000];
int k = 0;
public void addclass(int a)
{
cl[k] = new Class();
cl[k].setNumber(a);
k=k+1;
}
public Class searthclass(int a)
{
int k1 = 0;
while(k1<k) {
if(cl[k1].getNumber()==a )
return cl[k1];
k1 = k1 + 1;
}
return null;
}
public Class getClass(int a) {
return cl[a];
}
public void setClass1(Class class1,int a) {
this.cl[a] = class1;
}
}
abstract class Score {
protected int all =0;
protected int average = 0;
protected int usualgrades=0;
protected int finalgrades=0 ;
}
class Student {
private String name;
private int id;int n=0;
int k=0;
int c =0;public int getSumgrade() {
int sumgrade = 0;
for(int i =0;i<k;i++)
sumgrade+=sc[i].average;
return sumgrade;
}
private Score[] sc =new Score[1000] ;
private String[] cl =new String[1000];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public int usualgrades()
{
int all = 0;
for(int i =0;i<k;i++)
all=all+sc[i].usualgrades;
return all/k;
}
public int getaverage() {
int all=0;
for(int i =0;i<k;i++)
all+=sc[i].average;
if(all==0) return 0;
else return all/k;
}
public void setScore(Score score) {
sc[k] = score;
k ++;
}
public String getClass1(int a) {
return cl[a];
}
public void setClass1(String class1) {
cl[c] = class1;
c++;
}
public String search(String a)
{
for(int i=0;i<c;i++)
{
if(getClass1(i).equals(a))
{
return getClass1(i);}
}
return null;
}
public void setId(int id) {
this.id = id;
}
}
class Experimentgrades extends Score{
public Experimentgrades(int...grades)
{
for(int grade:grades)
average += grade;
average = average/grades.length;}
} - 设计与分析:
- 这题跟上面题比增加了一个
Experimentgrades:用来计算实验课成绩。
- 踩坑心得:
- 在计算班级平均分的时候直接把所有分数加起来再除于数量,其实应该先算每门的平均分在加起来除于数量,这个点到最后面同学提醒才发现,后面也来不及改。
改进建议:多分析分析题目再下手,别题目都没看清就写完了,这样会吃大亏。
7-2 课程成绩统计程序-3
类图:

源码:
import java.text.Collator;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String a = input.nextLine();
Collator co = Collator.getInstance(Locale.CHINA);
School school = new School();
Courseselectionsystem select = new Courseselectionsystem();
while (!a.equals("end")) {
Pattern pattern = Pattern.compile("[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)(必修|选修|实验)\\s(?!\\s)(考试|考察|实验)");
Matcher m = pattern.matcher(a);
if (m.find()) {
String[] number = a.split("\\s+");
if ((number[1].equals("必修") && (number[2].equals("考察") | number[2].equals("实验"))) | (number[1].equals("选修") && number[2].equals("实验")) | (number[1].equals("实验") && (number[2].equals("考试") | (number[2].equals("考察"))))) {
System.out.println(number[0] + " : course type & access mode mismatch");
} else if (number.length == 3) {
if (select.searthCourse(number[0]) == null)
select.addCourse(number[0], number[1], number[2]);
} else if (number.length == 5) {
if (Double.parseDouble(number[3]) + Double.parseDouble(number[4]) -1>=0.001&&select.searthCourse(number[0])==null)
System.out.println(number[0] + " : weight value error");
else
select.addCourseandweight(number[0], number[1], number[2], Double.parseDouble(number[3]), Double.parseDouble(number[4]));
} else if (number.length > 5) {
if (Integer.parseInt(number[3]) > 9) System.out.println("wrong format");
else {
if (number.length - 4 != Integer.parseInt(number[3]) && select.searthCourse(number[0]) == null) {
System.out.println(number[0] + " : number of scores does not match");
} else {
double sum = 0;
for (int i =4; i < number.length; i++) {
sum += Double.parseDouble(number[i]);
}
if (1-sum>= 0.01 && select.searthCourse(number[0]) == null)
System.out.println(number[0] + " : weight value error");
else {
Double[] weiht = new Double[Integer.parseInt(number[3])];
for (int i = 0; i < Integer.parseInt(number[3]); i++) {
weiht[i] = Double.parseDouble(number[i + 4]);
}
if (select.searthCourse(number[0]) == null)
select.addCourseandweight(number[0], number[1], number[2], weiht);
}
}
}
}
}
else {
pattern = Pattern.compile("[0-9]{8}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}\\s(?!\\s)[\u4E00-\u9FA5A-Za-z]{1,10}");
m = pattern.matcher(a);
if (m.find()) {
String[] number = a.split("\\s+");
if (select.searthCourse(number[2]) != null) {
if(select.searthCourse(number[2]).assessmentmethod.equals("考察"))
{
if(number.length!=4)
{System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (school.searthclass(Integer.parseInt(number[0]) / 100) == null)
school.addclass(Integer.parseInt(number[0]) / 100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0], number[1]);}
else {
if(school.searthclass(Integer.parseInt(number[0])/100)!=null)
{if(school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]))==null)
{school.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(Integer.parseInt(number[3]));}
else if(school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null) {
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]) ).setClass1(number[2]);
school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).setScore(Integer.parseInt(number[3]));
}
}
else {school.addclass(Integer.parseInt(number[0])/100);
school.searthclass(Integer.parseInt(number[0])/100).addStudent(number[0],number[1],number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(Integer.parseInt(number[3]));
}
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
{
select.addStudent(number[0], number[1], number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0]) ).setScore(Integer.parseInt(number[3]));
select.searthStudent(number[1],Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(Integer.parseInt(number[3]));
select.searthCourse(number[2]).n++;
select.searthCourse(number[2]).setScores(Integer.parseInt(number[3]));
select.searthCourse(number[2]).n++;}
else if(select.searthStudent(number[1],Integer.parseInt(number[0])).search(number[2])==null) {
select.searthCourse(number[2]).setScores(Integer.parseInt(number[3]));
select.searthCourse(number[2]).n++;
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(Integer.parseInt(number[3]));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
}
}
}
else if(select.searthCourse(number[2]).assessmentmethod.equals("考试"))
{
if(number.length!=5)
{
System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (school.searthclass(Integer.parseInt(number[0]) / 100) == null)
school.addclass(Integer.parseInt(number[0]) / 100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0], number[1]);
}
else {
int grade[] =new int[2];
for(int i=0;i<2;i++)
grade[i] = Integer.parseInt(number[3+i]);
school(school, select, number, grade);
if (select.searthStudent(number[1], Integer.parseInt(number[0])) == null) {
select.addStudent(number[0], number[1], number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthCourse(number[2]).n++;
} else if (select.searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null)
{ select.searthCourse(number[2]).setScores(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthCourse(number[2]).n++;
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);
select.searthStudent(number[1], Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++; }
}
}
else if(select.searthCourse(number[2]).assessmentmethod.equals("实验"))
{
if(number.length!=select.searthCourse(number[2]).getWeight().length+3)
{
System.out.println(number[0] + " " + number[1] + " : access mode mismatch");
if (school.searthclass(Integer.parseInt(number[0]) / 100) == null)
school.addclass(Integer.parseInt(number[0]) / 100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0], number[1]);
}
else {
int grade[] =new int[select.searthCourse(number[2]).getWeight().length];
for(int i=0;i<select.searthCourse(number[2]).getWeight().length;i++)
grade[i] = Integer.parseInt(number[3+i]);
school(school, select, number, grade);
if (select.searthStudent(number[1], Integer.parseInt(number[0])) == null) {
select.addStudent(number[0], number[1], number[2]);
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthCourse(number[2]).n++;
} else if (select.searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null) {
select.searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthStudent(number[1], Integer.parseInt(number[0])).n++;
select.searthCourse(number[2]).setScores(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
select.searthCourse(number[2]).n++;
select.searthStudent(number[1],Integer.parseInt(number[0])).setClass1(number[2]);
}
}
}
}
else {
System.out.println(number[2] + " does not exist");
if(school.searthclass(Integer.parseInt(number[0])/100)==null)
school.addclass(Integer.parseInt(number[0])/100);
if(select.searthStudent(number[1],Integer.parseInt(number[0]))==null)
select.addStudent2(number[0],number[1]);
}
}
else System.out.println("wrong format");
}
a= input.nextLine();
}
for(int i=0;i < select.s-1;i++)
{
for(int j = 0;j < select.s - i-1;j++)
{
if(select.getStudent(j).getId()>= select.getStudent(j+1).getId())
{
Student student1 = select.getStudent(j);
Student student2 =select.getStudent(j+1);
select.setStudent(student1,j+1);
select.setStudent(student2,j);
}
}
}
for(int i1=0;i1< select.s;i1++)
{
if(select.getStudent(i1).n!=0)
{
System.out.println(select.getStudent(i1).getId()+" "+select.getStudent(i1).getName()+" "+select.getStudent(i1).getaverage());
}
else System.out.println(select.getStudent(i1).getId()+" "+select.getStudent(i1).getName()+" did not take any exams");
} //课程名称的字符顺序排序
for(int i=0;i < select.c-1;i++)
{
for(int j = 0;j < select.c - i-1;j++)
{
if(co.compare(select.getCourse(j).name,select.getCourse(j+1).name)>=0 )
{
Course course1 = select.getCourse(j);
Course course2 =select.getCourse(j+1);
select.setCourse(course1,j+1);
select.setCourse(course2,j);
}
}
}
for(int i1=0;i1< select.c;i1++)
{
if(select.getCourse(i1).n!=0)
{
System.out.println(select.getCourse(i1).name+" "+select.getCourse(i1).getaverage());
}
else System.out.println(select.getCourse(i1).name+" has no grades yet");
}//按照班级平均分排序
for(int i=0;i < school.k-1;i++)
{
for(int j = 0;j < school.k - i-1;j++)
{
if(school.getClass(j).getSumgrade()/school.getClass(j).getSumcourse()>school.getClass(j+1).getSumgrade()/school.getClass(j+1).getSumcourse())
{
Class class2 = school.getClass(j);
Class class3 = school.getClass(j+1);
school.setClass1(class2,j+1);
school.setClass1(class3,j);
}
}
}
for(int i=0;i< school.k;i++)
{
if(school.getClass(i).k!=0)
System.out.println(school.getClass(i).getNumber()+" "+school.getClass(i).getSumgrade()/school.getClass(i).getSumcourse());
else System.out.println(school.getClass(i).getNumber()+" has no grades yet");
}
}
private static void school(School school, Courseselectionsystem select, String[] number, int[] grade) {
if (school.searthclass(Integer.parseInt(number[0]) / 100) != null) {
if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])) == null) {
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
} else if (school.searthclass(Integer.parseInt(number[0]) / 100).searthStudent(number[1], Integer.parseInt(number[0])).search(number[2]) == null) {
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0]) ).setClass1(number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
}
} else {
school.addclass(Integer.parseInt(number[0]) / 100);
school.searthclass(Integer.parseInt(number[0]) / 100).addStudent(number[0], number[1], number[2]);
school.searthclass(Integer.parseInt(number[0])/100).searthStudent(number[1],Integer.parseInt(number[0])).setScore(new Score(new Itemizedgrades(grade,select.searthCourse(number[2]).getWeight())));
}
}
}
class Class {
private int nu;//班号
private Student[] st =new Student[100];//学生
int k = 0;//学生
private Course[] courses =new Course[100];//这个班考了哪几门课
int c = 0;//课程
public int getNumber() {
return nu;
}
public int getSumcourse() {
int sumcourse = 0;
for(int i=0;i<k;i++)
{
sumcourse += st[i].k;
}
return sumcourse;
}
public int getSumgrade()
{
int sumgrade = 0;
for(int i=0;i<k;i++)
{
sumgrade += st[i].getSumgrade();
}
return sumgrade;
}
public void setNumber(int nu) {
this.nu = nu;
}
public void addStudent(String xuehao,String name,String course) {
st[k] = new Student();
st[k].setName(name);
st[k].setId(Integer.parseInt(xuehao));
st[k].setClass1(course);
k++;
}
public Student searthStudent(String dishName, int id)
{
int s1 = 0;
while(s1<k) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
}
class Course {
String name;
String style;
String assessmentmethod;
Score[] sc =new Score[1000] ;
private Double[] weight;
public void setWeight(Double[] weight) {
this.weight = weight;
}
public Double[] getWeight() {
return weight;
}
public void setScores(int a) {
sc[k] = new Score();
sc[k].sumscore = a;
k ++;
}
int k =0;
int n = 0;
public void setScores(Score scores) {
this.sc[k] = scores;
k=k+1;
}
public int getaverage()
{int sum=0;
for(int i=0;i< k;i++)
{
sum+=sc[i].getSumscore();
}
if(sum==0) return 0;
return sum/k;
}
}
class Coursegrades {
public String name;
public Double[] weight;
}
class Courseselectionsystem {
Course[] co =new Course[1000];
Student[] st =new Student[1000];
int c = 0;
int s = 0;
public void addCourse(String name,String style,String assessmentmethod)
{
co[c] = new Course();
co[c].name=name;
co[c].style=style;
co[c].assessmentmethod=assessmentmethod;
c=c+1;
}
public void addCourseandweight(String name,String style,String assessmentmethod,Double...a)
{
co[c] = new Course();
co[c].name=name;
co[c].style=style;
co[c].assessmentmethod=assessmentmethod;
co[c].setWeight(a);
c=c+1;
}
public Course searthCourse(String dishName)//
{
int c1 = 0;
while(c1<c) {
if(co[c1].name.equals(dishName) )
return co[c1];
c1 = c1 + 1;
}
return null;
}
public void addStudent2(String xuehao,String name) {
st[s] = new Student();
st[s].setName(name);
st[s].setId(Integer.parseInt(xuehao));
s++;
}
public void addStudent(String xuehao,String name,String course) {
st[s] = new Student();
st[s].setName(name);
st[s].setClass1(course);
st[s].setId(Integer.parseInt(xuehao));
s++;
}
public Student searthStudent(String dishName, int id)//
{
int s1 = 0;
while(s1<s) {
if(st[s1].getName().equals(dishName)&&st[s1].getId()==id )
return st[s1];
s1 = s1 + 1;
}
return null;
}
public void setStudent(Student student, int a) {
this.st[a] = student;
}
public Course getCourse(int a) {
return co[a];
}
public void setCourse(Course course, int a) {
this.co[a] = course;
}
public Student getStudent(int a) {
return st[a];
}
}
class Itemizedgrades {
private int grade[];
private Double[] weight;
public Itemizedgrades(int[] grade, Double[] weight) {
this.grade = grade;
this.weight = weight;
}
public int[] getGrade() {
return grade;
}
public Double[] getWeight() {
return weight;
}
}
class School {
private Class[] cl= new Class[1000];
int k = 0;
public void addclass(int a)
{
cl[k] = new Class();
cl[k].setNumber(a);
k=k+1;
}
public Class searthclass(int a)
{
int k1 = 0;
while(k1<k) {
if(cl[k1].getNumber()==a )
return cl[k1];
k1 = k1 + 1;
}
return null;
}
public Class getClass(int a) {
return cl[a];
}
public void setClass1(Class class1, int a) {
this.cl[a] = class1;
}
}
class Score {
private Itemizedgrades itemizedgrades;
public Coursegrades[] course=new Coursegrades[100];
int c = 0;
int sumscore=0;
public Score() {
}
public Score(Itemizedgrades itemizedgrades) {
this.itemizedgrades = itemizedgrades;
}
public int getSumscore() {
double p=0;
if(sumscore==0)
{
for(int i=0;i<this.itemizedgrades.getWeight().length;i++)
{
p +=this.itemizedgrades.getWeight()[i]*this.itemizedgrades.getGrade()[i];
}
sumscore=(int)p;
}
return sumscore;
}
}
class Student {
private String name;
private int id;
int n=0;
int k=0;
int c =0;
private Score[] sc =new Score[1000] ;
private String[] cl =new String[1000];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public int getaverage()
{int sum=0;
for(int i=0;i< k;i++)
{
sum+=sc[i].getSumscore();
}
if(sum==0) return 0;
return sum/k;
}
public int getSumgrade() {
int sumgrade = 0;
for(int i =0;i<k;i++)
sumgrade+=sc[i].getSumscore();
return sumgrade;
}
public void setScore(Score score) {
sc[k] = score;
k ++;
}
public void setScore(int a) {
sc[k] = new Score();
sc[k].sumscore = a;
k ++;
}
public String getClass1(int a) {
return cl[a];
}
public void setClass1(String class1) {
cl[c] = class1;
c++;
}
public String search(String a)
{
for(int i=0;i<c;i++)
{
if(getClass1(i).equals(a))
{
return getClass1(i);}
}
return null;
}
public void setId(int id) {
this.id = id;
}
}
设计与分析:
后面解决这个问题后,还有很多点过不去,不知道什么原因,不知道哪里错了。

改进建议:这次留了很少的时间敲代码,以后应该多花些时间在编程上面,防止题目太难而无法完成。
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String[] number ={"abstract","assert","boolean","break","byte","case","catch", "char","class","const","continue","default","do","double","else", "enum","extends","false","final","finally","float", "for","goto","if","implements","import","instanceof", "int","interface","long","native","new","null","package", "private","protected","public","return","short","static", "strictfp","super","switch","synchronized","this","throw", "throws","transient","true","try","void","volatile","while"};
Map<String,Integer> map =new HashMap<String, Integer>();
Set<String> set = new HashSet<>();
for(int i = 0;i < 53;i++)
{map.put(number[i],0);
set.add(number[i]);}
Scanner input = new Scanner(System.in);
String a = input.nextLine();
Boolean flag = false,flag1 = false;
String b ="";
while (!a.equals("exit"))
{
b += a.replaceAll("(?s)\\/\\/.*|\\/\\*.*?\\*/", " ").replaceAll("\\."," ").replaceAll("\"[^\"]*\""," ").replaceAll("\\("," ").replaceAll("\\)"," ").replaceAll("\\["," ").replaceAll("\\]"," ").replaceAll("\\;"," ").replaceAll("\\{"," ").replaceAll("\\}"," ").replaceAll("\\,"," ");
flag = true;
a = input.nextLine();
}
b=b.replaceAll("(?s)/\\*.*?\\*/", " ");
if(flag == false) System.out.print("Wrong Format");
else {
String[] number1 = b.split("\\s+");
for (int j = 0; j < number1.length; j++) {
if (set.contains(number1[j]))
map.put(number1[j], map.get(number1[j]) + 1);
}
for (int j = 0; j < map.size(); j++) {
if (map.get(number[j]) != 0) {
flag1 = true;
System.out.println(map.get(number[j]) + "\t" + number[j]);
}
}
if(flag1 == false) System.out.println("null");
}
}
}
设计与分析:这道题主要先把多行字符串加到一行,再去掉双引号和注释里的字符串,再根据Map来统计关键字出现的次数。
踩坑心得:没考虑(),.[]里面的字符串
后面全用replaceall给解决了。
改进建议:多考虑考虑有没有什么特殊情况,往往总有些特殊情况会被人给忽略掉。
总结:
掌握了queue,stack,line,set,map等一系列函数,暂时对set和map了解的还比较浅,对set和map排序还不太会,这部分还需要深度的学习,具体正则表达式的用法还需要研究。对于每次pta太难的题希望结束后能给所有测试点的测试样例和答案,这样在作业结束后能够及时的发现自己存在的问题,也不用盲目去找自己哪里存在问题,除此之外,课堂上讲的东西不是每次都有及时的练习,这样会导致上课讲的东西容易忘记。