第二次写博客(谁家博客总喜欢过去那么久再写啊)
- 前言
- 知识点:implements Comparable以及对改接口方法的补写,ArrayList相关内容
- 题量 :还可以
- 难度:这次学生管理系统的考察,总体分数要比菜单系列要高,其实主要原因归结于L老师在上课的时候讲了一下题目,并且(!)附赠了字符串解析类相关的代码,有了字符串解析接下来的操作就很简单了,但是第一次我完整的编写出来了代码,并且有考虑比如课程输入不能覆盖,以及各种其他特殊情形的处理(具体也记不太清了,毕竟过去了这么久),但是第一次只有58(有很多非零返回),没有及格,但是第二次新增了一个实验,我随便改了下就有九十多分了
- 设计与分析
- 系统1
- 类图
2.源代码
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.text.Collator;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String s_record = s.nextLine();
ParseInput handle = new ParseInput();
while (!s_record.equals("end")) {
handle.parseInput(s_record);// 解析用户输入的每一行数据
s_record = s.nextLine();
}
handle.showStudents();
handle.showCourses();
handle.showClasses();
}
}
class Class implements Comparable{
String num;
int averg;
Student stu;
Class(String num,int averg)
{
this.averg = averg;
this.num = num;
}
Class(String num)
{
this.num = num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int getAverg() {
return averg;
}
public void setAverg(int averg) {
this.averg = averg;
}
@Override
// public int compareTo(Object arg) {
// // TODO Auto-generated method stub
// if(arg instanceof Class){
// Class goods = (Class) arg;
// if(this.averg<goods.averg) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Class){
Class goods = (Class) o;
if(this.num.compareTo(goods.num)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
class Course implements Comparable{
String type;
String name;
String bcho;
int ccave;
int nnave;
int ffave;
Course(String name,String type,String bcho)
{
this.bcho = bcho;
this.name = name;
this.type = type;
}
public String getBcho() {
return bcho;
}
public void setBcho(String bcho) {
this.bcho = bcho;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// @Override
// public int compareTo(Object 0) {
// // TODO Auto-generated method stub
// if(o instanceof Course){
// Course goods = (Course) o;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
// }
// public int compareTo(Object agro) {
// // TODO Auto-generated method stub
// if(agro instanceof Course){
// Course goods = (Course) agro;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Course){
Course goods = (Course) o;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
return compare.compare(name,goods.name);
}
throw new RuntimeException("传入的数据类型不一致");
}
public int compareTo(Course o) {
Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
return compare.compare(name,o.getName());
}
}
class InputMatching {
static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
static String scoreMatching = "([1-9]?[0-9]|100)";
static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
static String courseTypeMatching = "(选修|必修)";
static String checkcourseTypeMatching = "(考试|考察)";
//cousrInput用于定义课程信息模式(正则表达式)
static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching;
//scoreInput用于定义成绩信息模式(正则表达式)
static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
scoreMatching + "(scoreMatching)?";
public static int matchingInput(String s) {
if (matchingCourse(s)) {
return 1;
}
if (matchingScore(s)) {
return 2;
}
return 0;
}
private static boolean matchingCourse(String s) {
return s.matches(courseInput);
}
private static boolean matchingScore(String s) {
//System.out.println(match);
return s.matches(scoreInput);
}
}
class ParseInput {
InputMatching im;
ArrayList<Course> classlist = new ArrayList<>();
ArrayList<Student> stulist = new ArrayList<>();
ArrayList<Class> clist = new ArrayList<>();
ArrayList<St> ss = new ArrayList<>();
public void parseInput(String s_record) {
// TODO Auto-generated method stub
if (im.matchingInput(s_record) == 1) {
String[] a = s_record.split(" ");
if (a[1].equals("必修") == true && a[2].equals("考察") == true) {
System.out.println(a[0] + " : course type & access mode mismatch");
return;
}
Course cls = new Course(a[0], a[1], a[2]);
if (judecls(classlist, a[0]) == null) {
classlist.add(cls);
}
} else {
String[] a = s_record.split(" ");
if (a.length == 4) {
Course clss;
clss = judecls(classlist, a[2]);
if (clss == null) {
// System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist");
// 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
System.out.println(a[2] + " does not exist");
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
if (clss.bcho.equals("考试") == true) {
System.out.println(a[0] + " " + a[1] + " : access mode mismatch");
// 学号+英文空格+姓名+英文空格+": access mode mismatch"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
int grade;
grade = Integer.valueOf(a[3]);
if(grade>100||grade<0)
{
System.out.println("wrong format");
return;
}
Student stu = new Student(a[0], a[1], clss, grade);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
stu.setfinal(grade);
stulist.add(stu);
dd(a[0],a[1]);
}
}
} else {
Course clss;
clss = judecls(classlist, a[2]);
if (clss == null) {
// System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist");
System.out.println(a[2] + " does not exist");
// 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
if (clss.bcho.equals("考核") == true) {
System.out.println(a[0] + " " + a[1] + " : access mode mismatch");
// 学号+英文空格+姓名+英文空格+": access mode mismatch"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
int egrade, ngrade;
ngrade = Integer.valueOf(a[3]);
egrade = Integer.valueOf(a[4]);
if(ngrade>100||egrade<0||ngrade<0||egrade>100)
{
System.out.println("wrong format");
return;
}
Student stu = new Student(a[0], a[1], clss, egrade, ngrade);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
stu.setFinal(egrade, ngrade);
stulist.add(stu);
dd(a[0],a[1]);
}
}
}
}
}
public Course judecls(ArrayList<Course> cls, String name) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).name.equals(name) == true)
return cls.get(i);
}
return null;
}
public Class jude(ArrayList<Class> cls, String num) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).num.equals(num) == true)
return cls.get(i);
}
return null;
}
public St jugde(ArrayList<St> cls, String id) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).id.equals(id) == true)
return cls.get(i);
}
return null;
}
public void dd(String s,String n)
{
St stt = new St(s,n);
if(jugde(ss,s)==null)
ss.add(stt);
}
public void showStudents() {
// TODO Auto-generated method stub
// Collections.sort(stulist);
// for (int i = 0; i < stulist.size(); i++) {
// Student st = stulist.get(i);
// System.out.println(st.id + " " + st.name + " " + st.final_grade);
// }
int sum = 0;
int n = 0;
for (int i = 0; i < ss.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (ss.get(i).id.equals(stulist.get(j).id) == true) {
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(ss.get(i).id+" "+ss.get(i).name + " did not take any exams");
else {
ss.get(i).aevv = sum / n;
}
sum = 0;
n = 0;
}
Collections.sort(ss);
for (int i = 0; i < ss.size(); i++) {
St cs = ss.get(i);
if(cs.aevv!=0)
System.out.println(cs.id+" "+cs.name+" "+cs.aevv);
}
}
public void showCourses() {
// TODO Auto-generated method stub
int sum = 0;
int n = 0;
int esum = 0;
int nsum = 0;
for (int i = 0; i < classlist.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (classlist.get(i).name.equals(stulist.get(j).cls.name) == true) {
if (classlist.get(i).bcho.equals("考试") == true) {
nsum = nsum + stulist.get(j).nor_grade;
}
esum = esum + stulist.get(j).exa_grade;
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(classlist.get(i).name + " has no grades yet");
else {
classlist.get(i).ccave = sum / n;
classlist.get(i).ffave = esum / n;
if (classlist.get(i).bcho.equals("考试") == true)
classlist.get(i).nnave = nsum / n;
}
sum = 0;
n = 0;
esum = 0;
nsum = 0;
}
Collections.sort(classlist);
// for(int i =0;i<classlist.size()-1;i++)
// {
// Course cc = classlist.get(i);
// cc.compareTo(classlist.get(i+1));
// }
for (int i = 0; i < classlist.size(); i++) {
Course cs = classlist.get(i);
if(cs.ffave!=0)
{
if(classlist.get(i).bcho.equals("考试") == true)
System.out.println(cs.name+" "+cs.nnave+" "+cs.ffave+" "+cs.ccave);
else
System.out.println(cs.name+" "+cs.ffave+" "+cs.ccave);
}
}
}
public void showClasses() {
//TODO Auto-generated method stub
int sum = 0;
int n = 0;
for (int i = 0; i < clist.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (clist.get(i).num.equals(stulist.get(j).id.substring(0, 6)) == true) {
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(clist.get(i).num + " has no grades yet");
else {
clist.get(i).averg = sum / n;
}
sum = 0;
n = 0;
}
Collections.sort(clist);
for (int i = 0; i < clist.size(); i++) {
Class cs = clist.get(i);
if(cs.averg!=0)
System.out.println(cs.num+" "+cs.averg);
}
}
}
class St implements Comparable{
String id;
Student stu;
String name;
int aevv;
St(String id,String name)
{
this.id=id;
this.name = name;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof St){
St goods = (St) o;
if(this.id.compareTo(goods.id)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
class Student implements Comparable{
Course cls;
String id;
String name;
int final_grade;
int nor_grade;
int exa_grade;
int avv;
Student(String id,String name,Course cls,int exa_grade)
{
this.name = name;
this.cls = cls;
this.id=id;
this.exa_grade = exa_grade;
}
Student(String id,String name,Course cls,int exa_grade,int nor_grade)
{
this.name = name;
this.cls = cls;
this.id=id;
this.exa_grade = exa_grade;
this.nor_grade = nor_grade;
}
public void setfinal(int exa_grade)
{
final_grade = exa_grade;
}
public void setFinal(int exa_grade,int nor_grade)
{
final_grade = (int)(exa_grade*0.7 + nor_grade*0.3);
}
public Course getCls() {
return cls;
}
public void setCls(Course cls) {
this.cls = cls;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFinal_grade() {
return final_grade;
}
public void setFinal_grade(int final_grade) {
this.final_grade = final_grade;
}
public int getNor_grade() {
return nor_grade;
}
public void setNor_grade(int nor_grade) {
this.nor_grade = nor_grade;
}
public int getExa_grade() {
return exa_grade;
}
public void setExa_grade(int exa_grade) {
this.exa_grade = exa_grade;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Student){
Student goods = (Student) o;
if(this.id.compareTo(goods.id)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
-
- 系统2
- 类图
2.代码
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.text.Collator;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String s_record = s.nextLine();
ParseInput handle = new ParseInput();
while (!s_record.equals("end")) {
handle.parseInput(s_record);// 解析用户输入的每一行数据
s_record = s.nextLine();
}
handle.showStudents();
handle.showCourses();
handle.showClasses();
}
}
class Class implements Comparable{
String num;
int averg;
Student stu;
Class(String num,int averg)
{
this.averg = averg;
this.num = num;
}
Class(String num)
{
this.num = num;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public int getAverg() {
return averg;
}
public void setAverg(int averg) {
this.averg = averg;
}
@Override
// public int compareTo(Object arg) {
// // TODO Auto-generated method stub
// if(arg instanceof Class){
// Class goods = (Class) arg;
// if(this.averg<goods.averg) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Class){
Class goods = (Class) o;
if(this.num.compareTo(goods.num)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
class Course implements Comparable{
String type;
String name;
String bcho;
int ccave;
int nnave;
int ffave;
Course(String name,String type,String bcho)
{
this.bcho = bcho;
this.name = name;
this.type = type;
}
public String getBcho() {
return bcho;
}
public void setBcho(String bcho) {
this.bcho = bcho;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// @Override
// public int compareTo(Object 0) {
// // TODO Auto-generated method stub
// if(o instanceof Course){
// Course goods = (Course) o;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
// }
// public int compareTo(Object agro) {
// // TODO Auto-generated method stub
// if(agro instanceof Course){
// Course goods = (Course) agro;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
// }
// throw new RuntimeException("传入的数据类型不一致");
// }
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Course){
Course goods = (Course) o;
// if(this.ccave<goods.ccave) return -1;
// else
// return 1;
Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
return compare.compare(name,goods.name);
}
throw new RuntimeException("传入的数据类型不一致");
}
public int compareTo(Course o) {
Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
return compare.compare(name,o.getName());
}
}
class InputMatching {
static String stuNumMatching = "[0-9]{8}";//8个0-9的数字
static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
static String scoreMatching = "([1-9]?[0-9]|100)";
static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符
static String courseTypeMatching = "(选修|必修|实验)";
static String checkcourseTypeMatching = "(考试|考察|实验)";
//cousrInput用于定义课程信息模式(正则表达式)
static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching;
//scoreInput用于定义成绩信息模式(正则表达式)
static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
scoreMatching + "(scoreMatching)?";
public static int matchingInput(String s) {
if (matchingCourse(s)) {
return 1;
}
if (matchingScore(s)) {
return 2;
}
return 0;
}
private static boolean matchingCourse(String s) {
return s.matches(courseInput);
}
private static boolean matchingScore(String s) {
//System.out.println(match);
return s.matches(scoreInput);
}
}
class ParseInput {
InputMatching im;
ArrayList<Course> classlist = new ArrayList<>();
ArrayList<Student> stulist = new ArrayList<>();
ArrayList<Class> clist = new ArrayList<>();
ArrayList<St> ss = new ArrayList<>();
public void parseInput(String s_record) {
// TODO Auto-generated method stub
String[] swd = s_record.split(" ");
if (im.matchingInput(s_record) == 1) {
String[] a = s_record.split(" ");
if (a[1].equals("必修") == true && a[2].equals("考察") == true) {
System.out.println(a[0] + " : course type & access mode mismatch");
return;
}
if(a[1].equals("实验")==true&&a[2].equals("实验")==false)
{
System.out.println(a[0] + " : course type & access mode mismatch");
return;
}
if(a[2].equals("实验")==true&&a[1].equals("实验")==false)
{
System.out.println(a[0] + " : course type & access mode mismatch");
return;
}
Course cls = new Course(a[0], a[1], a[2]);
if (judecls(classlist, a[0]) == null) {
classlist.add(cls);
}
}
if(im.matchingInput(s_record) !=1&&swd.length<=5) {
String[] a = s_record.split(" ");
if (a.length == 4) {
Course clss;
clss = judecls(classlist, a[2]);
if (clss == null) {
System.out.println(a[2] + " does not exist");
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
}
else {
if (clss.bcho.equals("考试") == true) {
System.out.println(a[0] + " " + a[1] + " : access mode mismatch");
// 学号+英文空格+姓名+英文空格+": access mode mismatch"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
int grade;
grade = Integer.valueOf(a[3]);
if(grade>100||grade<0)
{
System.out.println("wrong format");
return;
}
Student stu = new Student(a[0], a[1], clss, grade);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
stu.setfinal(grade);
stulist.add(stu);
dd(a[0],a[1]);
}
}
}
else {
Course clss;
clss = judecls(classlist, a[2]);
if (clss == null) {
System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist");
System.out.println(a[2] + " does not exist");
// 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
if (clss.bcho.equals("考核") == true) {
//System.out.println(a[0] + " " + a[1] + " : access mode mismatch");
// 学号+英文空格+姓名+英文空格+": access mode mismatch"
dd(a[0],a[1]);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
return;
} else {
int egrade, ngrade;
ngrade = Integer.valueOf(a[3]);
egrade = Integer.valueOf(a[4]);
if(ngrade>100||egrade<0||ngrade<0||egrade>100)
{
System.out.println("wrong format");
return;
}
Student stu = new Student(a[0], a[1], clss, egrade, ngrade);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
stu.setFinal(egrade, ngrade);
stulist.add(stu);
dd(a[0],a[1]);
}
}
}
}
if(im.matchingInput(s_record) !=1&&swd.length>5)
{
if(s_record.indexOf("end")!=-1)
return;
String[] a = s_record.split(" ");
int n = a.length - 4;
int num = Integer.valueOf(a[3]);
if(num==3)
{
System.out.println("wrong format");
return;
}
if(n!=num)
{
System.out.println(a[0]+" "+a[1]+" : access mode mismatch");
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
dd(a[0],a[1]);
return;
}
int[] x = new int[num];
int sum = 0;
for(int i=0;i<num;i++)
{
x[i]= Integer.valueOf(a[4+i]);
sum = sum +x[i];
if(x[i]>100||x[i]<0)
{
System.out.println("wrong format");
return;
}
}
Course clss;
clss = judecls(classlist, a[2]);
if (clss == null) {
// System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist");
// 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
System.out.println(a[2] + " does not exist");
}
Student st = new Student(a[0],a[1],clss,sum/num);
if(jude(clist,a[0].substring(0,6))==null)
{
Class cl = new Class(a[0].substring(0,6));
clist.add(cl);
}
stulist.add(st);
st.setFinal_grade(sum/num);
dd(a[0],a[1]);
}
}
public Course judecls(ArrayList<Course> cls, String name) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).name.equals(name) == true)
return cls.get(i);
}
return null;
}
public Class jude(ArrayList<Class> cls, String num) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).num.equals(num) == true)
return cls.get(i);
}
return null;
}
public St jugde(ArrayList<St> cls, String id) {
for (int i = 0; i < cls.size(); i++) {
if (cls.get(i).id.equals(id) == true)
return cls.get(i);
}
return null;
}
public void dd(String s,String n)
{
St stt = new St(s,n);
if(jugde(ss,s)==null)
ss.add(stt);
}
public void showStudents() {
// TODO Auto-generated method stub
// Collections.sort(stulist);
// for (int i = 0; i < stulist.size(); i++) {
// Student st = stulist.get(i);
// System.out.println(st.id + " " + st.name + " " + st.final_grade);
// }
int sum = 0;
int n = 0;
for (int i = 0; i < ss.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (ss.get(i).id.equals(stulist.get(j).id) == true) {
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(ss.get(i).id+" "+ss.get(i).name + " did not take any exams");
else {
ss.get(i).aevv = sum / n;
}
sum = 0;
n = 0;
}
Collections.sort(ss);
for (int i = 0; i < ss.size(); i++) {
St cs = ss.get(i);
if(cs.aevv!=0)
System.out.println(cs.id+" "+cs.name+" "+cs.aevv);
}
}
public void showCourses() {
// TODO Auto-generated method stub
int sum = 0;
int n = 0;
int esum = 0;
int nsum = 0;
for (int i = 0; i < classlist.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (classlist.get(i).name.equals(stulist.get(j).cls.name) == true) {
if (classlist.get(i).bcho.equals("考试") == true) {
nsum = nsum + stulist.get(j).nor_grade;
}
esum = esum + stulist.get(j).exa_grade;
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(classlist.get(i).name + " has no grades yet");
else {
classlist.get(i).ccave = sum / n;
classlist.get(i).ffave = esum / n;
if (classlist.get(i).bcho.equals("考试") == true)
classlist.get(i).nnave = nsum / n;
}
sum = 0;
n = 0;
esum = 0;
nsum = 0;
}
Collections.sort(classlist);
// for(int i =0;i<classlist.size()-1;i++)
// {
// Course cc = classlist.get(i);
// cc.compareTo(classlist.get(i+1));
// }
for (int i = 0; i < classlist.size(); i++) {
Course cs = classlist.get(i);
if(cs.ffave!=0)
{
if(classlist.get(i).bcho.equals("考试") == true)
System.out.println(cs.name+" "+cs.nnave+" "+cs.ffave+" "+cs.ccave);
if(classlist.get(i).bcho.equals("考察") == true)
System.out.println(cs.name+" "+cs.ffave+" "+cs.ccave);
if(classlist.get(i).bcho.equals("实验") == true)
System.out.println(cs.name+" "+cs.ccave);
}
}
}
public void showClasses() {
//TODO Auto-generated method stub
int sum = 0;
int n = 0;
for (int i = 0; i < clist.size(); i++) {
for (int j = 0; j < stulist.size(); j++) {
if (clist.get(i).num.equals(stulist.get(j).id.substring(0, 6)) == true) {
sum = sum + stulist.get(j).final_grade;
n++;
}
}
if (n == 0)
System.out.println(clist.get(i).num + " has no grades yet");
else {
clist.get(i).averg = sum / n;
}
sum = 0;
n = 0;
}
Collections.sort(clist);
for (int i = 0; i < clist.size(); i++) {
Class cs = clist.get(i);
if(cs.averg!=0)
System.out.println(cs.num+" "+cs.averg);
}
}
}
class St implements Comparable{
String id;
Student stu;
String name;
int aevv;
St(String id,String name)
{
this.id=id;
this.name = name;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof St){
St goods = (St) o;
if(this.id.compareTo(goods.id)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
class Student implements Comparable{
Course cls;
String id;
String name;
int final_grade;
int nor_grade;
int exa_grade;
int avv;
Student(String id,String name,Course cls,int exa_grade)
{
this.name = name;
this.cls = cls;
this.id=id;
this.exa_grade = exa_grade;
}
Student(String id,String name,Course cls,int exa_grade,int nor_grade)
{
this.name = name;
this.cls = cls;
this.id=id;
this.exa_grade = exa_grade;
this.nor_grade = nor_grade;
}
// Student(String id,String name,Course cls,int final_grade)
// {
// this.name = name;
// this.cls = cls;
// this.id=id;
// this.final_grade = final_grade;
// }
public void setfinal(int exa_grade)
{
final_grade = exa_grade;
}
public void setFinal(int exa_grade,int nor_grade)
{
final_grade = (int)(exa_grade*0.7 + nor_grade*0.3);
}
public Course getCls() {
return cls;
}
public void setCls(Course cls) {
this.cls = cls;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFinal_grade() {
return final_grade;
}
public void setFinal_grade(int final_grade) {
this.final_grade = final_grade;
}
public int getNor_grade() {
return nor_grade;
}
public void setNor_grade(int nor_grade) {
this.nor_grade = nor_grade;
}
public int getExa_grade() {
return exa_grade;
}
public void setExa_grade(int exa_grade) {
this.exa_grade = exa_grade;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
if(o instanceof Student){
Student goods = (Student) o;
if(this.id.compareTo(goods.id)<0) return -1;
else
return 1;
}
throw new RuntimeException("传入的数据类型不一致");
}
}
-
- 系统3
源代码
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.text.Collator; import java.util.Comparator; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s_record = s.nextLine(); ParseInput handle = new ParseInput(); while (!s_record.equals("end")) { handle.parseInput(s_record);// 解析用户输入的每一行数据 s_record = s.nextLine(); } handle.showStudents(); handle.showCourses(); handle.showClasses(); } } class Class implements Comparable{ String num; int averg; Student stu; Class(String num,int averg) { this.averg = averg; this.num = num; } Class(String num) { this.num = num; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public int getAverg() { return averg; } public void setAverg(int averg) { this.averg = averg; } @Override // public int compareTo(Object arg) { // // TODO Auto-generated method stub // if(arg instanceof Class){ // Class goods = (Class) arg; // if(this.averg<goods.averg) return -1; // else // return 1; // } // throw new RuntimeException("传入的数据类型不一致"); // } public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof Class){ Class goods = (Class) o; if(this.num.compareTo(goods.num)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } } class Course implements Comparable{ String type; String name; String bcho; int ccave; int nnave; int ffave; Course(String name,String type,String bcho) { this.bcho = bcho; this.name = name; this.type = type; } public String getBcho() { return bcho; } public void setBcho(String bcho) { this.bcho = bcho; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } // @Override // public int compareTo(Object 0) { // // TODO Auto-generated method stub // if(o instanceof Course){ // Course goods = (Course) o; // if(this.ccave<goods.ccave) return -1; // else // return 1; // } // throw new RuntimeException("传入的数据类型不一致"); // } // } // public int compareTo(Object agro) { // // TODO Auto-generated method stub // if(agro instanceof Course){ // Course goods = (Course) agro; // if(this.ccave<goods.ccave) return -1; // else // return 1; // } // throw new RuntimeException("传入的数据类型不一致"); // } public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof Course){ Course goods = (Course) o; // if(this.ccave<goods.ccave) return -1; // else // return 1; Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(name,goods.name); } throw new RuntimeException("传入的数据类型不一致"); } public int compareTo(Course o) { Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA); return compare.compare(name,o.getName()); } } class InputMatching { static String stuNumMatching = "[0-9]{8}";//8个0-9的数字 static String stuNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String scoreMatching = "([1-9]?[0-9]|100)"; static String courseNameMatching = "\\S{1,10}";//1到10个非空格(TAB)字符 static String courseTypeMatching = "(选修|必修|实验)"; static String checkcourseTypeMatching = "(考试|考察|实验)"; //cousrInput用于定义课程信息模式(正则表达式) static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkcourseTypeMatching; //scoreInput用于定义成绩信息模式(正则表达式) static String scoreInput = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " + scoreMatching + "(scoreMatching)?"; public static int matchingInput(String s) { if (matchingCourse(s)) { return 1; } if (matchingScore(s)) { return 2; } return 0; } private static boolean matchingCourse(String s) { return s.matches(courseInput); } private static boolean matchingScore(String s) { //System.out.println(match); return s.matches(scoreInput); } } class ParseInput { InputMatching im; ArrayList<Course> classlist = new ArrayList<>(); ArrayList<Student> stulist = new ArrayList<>(); ArrayList<Class> clist = new ArrayList<>(); ArrayList<St> ss = new ArrayList<>(); public void parseInput(String s_record) { // TODO Auto-generated method stub String[] swd = s_record.split(" "); if (im.matchingInput(s_record) == 1) { String[] a = s_record.split(" "); if (a[1].equals("必修") == true && a[2].equals("考察") == true) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } if(a[1].equals("实验")==true&&a[2].equals("实验")==false) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } if(a[2].equals("实验")==true&&a[1].equals("实验")==false) { System.out.println(a[0] + " : course type & access mode mismatch"); return; } Course cls = new Course(a[0], a[1], a[2]); if (judecls(classlist, a[0]) == null) { classlist.add(cls); } } if(im.matchingInput(s_record) !=1&&swd.length<=5) { String[] a = s_record.split(" "); if (a.length == 4) { Course clss; clss = judecls(classlist, a[2]); if (clss == null) { System.out.println(a[2] + " does not exist"); dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { if (clss.bcho.equals("考试") == true) { System.out.println(a[0] + " " + a[1] + " : access mode mismatch"); // 学号+英文空格+姓名+英文空格+": access mode mismatch" dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { int grade; grade = Integer.valueOf(a[3]); if(grade>100||grade<0) { System.out.println("wrong format"); return; } Student stu = new Student(a[0], a[1], clss, grade); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stu.setfinal(grade); stulist.add(stu); dd(a[0],a[1]); } } } else { Course clss; clss = judecls(classlist, a[2]); if (clss == null) { System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist"); System.out.println(a[2] + " does not exist"); // 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist" dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { if (clss.bcho.equals("考核") == true) { //System.out.println(a[0] + " " + a[1] + " : access mode mismatch"); // 学号+英文空格+姓名+英文空格+": access mode mismatch" dd(a[0],a[1]); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } return; } else { int egrade, ngrade; ngrade = Integer.valueOf(a[3]); egrade = Integer.valueOf(a[4]); if(ngrade>100||egrade<0||ngrade<0||egrade>100) { System.out.println("wrong format"); return; } Student stu = new Student(a[0], a[1], clss, egrade, ngrade); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stu.setFinal(egrade, ngrade); stulist.add(stu); dd(a[0],a[1]); } } } } if(im.matchingInput(s_record) !=1&&swd.length>5) { if(s_record.indexOf("end")!=-1) return; String[] a = s_record.split(" "); int n = a.length - 4; int num = Integer.valueOf(a[3]); if(num==3) { System.out.println("wrong format"); return; } if(n!=num) { System.out.println(a[0]+" "+a[1]+" : access mode mismatch"); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } dd(a[0],a[1]); return; } int[] x = new int[num]; int sum = 0; for(int i=0;i<num;i++) { x[i]= Integer.valueOf(a[4+i]); sum = sum +x[i]; if(x[i]>100||x[i]<0) { System.out.println("wrong format"); return; } } Course clss; clss = judecls(classlist, a[2]); if (clss == null) { // System.out.println(a[0] + " " + a[1] + " :" + a[2] + " does not exist"); // 学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist" System.out.println(a[2] + " does not exist"); } Student st = new Student(a[0],a[1],clss,sum/num); if(jude(clist,a[0].substring(0,6))==null) { Class cl = new Class(a[0].substring(0,6)); clist.add(cl); } stulist.add(st); st.setFinal_grade(sum/num); dd(a[0],a[1]); } } public Course judecls(ArrayList<Course> cls, String name) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).name.equals(name) == true) return cls.get(i); } return null; } public Class jude(ArrayList<Class> cls, String num) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).num.equals(num) == true) return cls.get(i); } return null; } public St jugde(ArrayList<St> cls, String id) { for (int i = 0; i < cls.size(); i++) { if (cls.get(i).id.equals(id) == true) return cls.get(i); } return null; } public void dd(String s,String n) { St stt = new St(s,n); if(jugde(ss,s)==null) ss.add(stt); } public void showStudents() { // TODO Auto-generated method stub // Collections.sort(stulist); // for (int i = 0; i < stulist.size(); i++) { // Student st = stulist.get(i); // System.out.println(st.id + " " + st.name + " " + st.final_grade); // } int sum = 0; int n = 0; for (int i = 0; i < ss.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (ss.get(i).id.equals(stulist.get(j).id) == true) { sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(ss.get(i).id+" "+ss.get(i).name + " did not take any exams"); else { ss.get(i).aevv = sum / n; } sum = 0; n = 0; } Collections.sort(ss); for (int i = 0; i < ss.size(); i++) { St cs = ss.get(i); if(cs.aevv!=0) System.out.println(cs.id+" "+cs.name+" "+cs.aevv); } } public void showCourses() { // TODO Auto-generated method stub int sum = 0; int n = 0; int esum = 0; int nsum = 0; for (int i = 0; i < classlist.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (classlist.get(i).name.equals(stulist.get(j).cls.name) == true) { if (classlist.get(i).bcho.equals("考试") == true) { nsum = nsum + stulist.get(j).nor_grade; } esum = esum + stulist.get(j).exa_grade; sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(classlist.get(i).name + " has no grades yet"); else { classlist.get(i).ccave = sum / n; classlist.get(i).ffave = esum / n; if (classlist.get(i).bcho.equals("考试") == true) classlist.get(i).nnave = nsum / n; } sum = 0; n = 0; esum = 0; nsum = 0; } Collections.sort(classlist); // for(int i =0;i<classlist.size()-1;i++) // { // Course cc = classlist.get(i); // cc.compareTo(classlist.get(i+1)); // } for (int i = 0; i < classlist.size(); i++) { Course cs = classlist.get(i); if(cs.ffave!=0) { if(classlist.get(i).bcho.equals("考试") == true) System.out.println(cs.name+" "+cs.nnave+" "+cs.ffave+" "+cs.ccave); if(classlist.get(i).bcho.equals("考察") == true) System.out.println(cs.name+" "+cs.ffave+" "+cs.ccave); if(classlist.get(i).bcho.equals("实验") == true) System.out.println(cs.name+" "+cs.ccave); } } } public void showClasses() { //TODO Auto-generated method stub int sum = 0; int n = 0; for (int i = 0; i < clist.size(); i++) { for (int j = 0; j < stulist.size(); j++) { if (clist.get(i).num.equals(stulist.get(j).id.substring(0, 6)) == true) { sum = sum + stulist.get(j).final_grade; n++; } } if (n == 0) System.out.println(clist.get(i).num + " has no grades yet"); else { clist.get(i).averg = sum / n; } sum = 0; n = 0; } Collections.sort(clist); for (int i = 0; i < clist.size(); i++) { Class cs = clist.get(i); if(cs.averg!=0) System.out.println(cs.num+" "+cs.averg); } } } class St implements Comparable{ String id; Student stu; String name; int aevv; St(String id,String name) { this.id=id; this.name = name; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof St){ St goods = (St) o; if(this.id.compareTo(goods.id)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } } class Student implements Comparable{ Course cls; String id; String name; int final_grade; int nor_grade; int exa_grade; int avv; Student(String id,String name,Course cls,int exa_grade) { this.name = name; this.cls = cls; this.id=id; this.exa_grade = exa_grade; } Student(String id,String name,Course cls,int exa_grade,int nor_grade) { this.name = name; this.cls = cls; this.id=id; this.exa_grade = exa_grade; this.nor_grade = nor_grade; } // Student(String id,String name,Course cls,int final_grade) // { // this.name = name; // this.cls = cls; // this.id=id; // this.final_grade = final_grade; // } public void setfinal(int exa_grade) { final_grade = exa_grade; } public void setFinal(int exa_grade,int nor_grade) { final_grade = (int)(exa_grade*0.7 + nor_grade*0.3); } public Course getCls() { return cls; } public void setCls(Course cls) { this.cls = cls; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getFinal_grade() { return final_grade; } public void setFinal_grade(int final_grade) { this.final_grade = final_grade; } public int getNor_grade() { return nor_grade; } public void setNor_grade(int nor_grade) { this.nor_grade = nor_grade; } public int getExa_grade() { return exa_grade; } public void setExa_grade(int exa_grade) { this.exa_grade = exa_grade; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if(o instanceof Student){ Student goods = (Student) o; if(this.id.compareTo(goods.id)<0) return -1; else return 1; } throw new RuntimeException("传入的数据类型不一致"); } }
- 踩坑心得
- 非管理系统中的踩坑:
- 在题目集二中一开始我认为只要算出来几月几日是这一年的第多少天然后再用这个第多少天%7的结果就是星期几,后来我发现有测试点一直过不了,然后翻了下日历才发现原来不是每年的1月1日都是星期一...改进方法是去csdn了搜了一下得了个简单的计算公式:
c = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
Y是年份数,D是这一天在这一年中的累积天数,也就是这一天在这一年中是第几天。
2.在题目集三中(7-3 去掉重复的数据),有一个测试点死活过不了,后来才知道是因为一个数据一个数据读太耗时了,最后选择将整个字符串读下来再进行操作解决了该问题
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] arr = str.split(" ");
3.另外就是封装性那道题目,因为上学期写C语言PTA函数题的影响(上学期写的函数题都是主体题目给了,只需要在PTA里补写函数体就可以了)导致我把所有类都写好了还是过不了,最后才发现是要把题干给的主体复制过去才行
-
- 管理中的踩坑:
先看一下图片就知道踩了多少坑了(主要是测试点没给出(好像是pta改版了然后当时我不知道怎么看测试样例))
1.感觉给了字符串解析的话就简单了很多,难的是不会踩点不知道自己的程序有什么问题
2.题干和测试样例不完全一致,比如特殊的情况输出不一致,还有排序方式不一致(说好的班级按平均分排呢
- 主要困难以及改进建议
- 困难:没有测试点不知道问题在哪,第一个管理系统在所给测试用例全过的情况下
- 改进建议:
- 于我自己:应该多花时间在PTA作业上,多思考代码的不足和可能出现的特殊情况
- 于老师:应该及时讲解题目或者发布相关测试点或标准题解,这样不至于让学生做完一次下一次还是这么差,还有就是给我测试点吧给我测试点吧orz,还有好好出题吧,为什么文字题干和测试用例都不一样的啊
- 总结
其实上面该总结的都总结的差不多了,最后还要说的话就是自己在java上面花的时间其实不算多,上课其实听的还挺认真的,但是下课回去因为没有相关练习,自己也不会主动去把上课那些东西再敲一遍,导致其实相关知识掌握的如何我自己都没有什么把握,对“面对对象编程”其实还是不太了解其内核本质,这样就比较可怕了,java比上学期c语言要难的多,但是更加智能,用起来更加方便,而我花点时间却远不如上学期那般,其中有各种原因而搁置了相关的学习,还是要踏踏实实去做的未来!!!
标签:return,String,get,int,BLOG,grade,public From: https://www.cnblogs.com/rrringo/p/17512524.html