(1)前言
前面的点菜系统结束后,以为终于解脱了,没想到是另一个噩梦。以下是PTA6到8的相关问题。
题目集6,只有一题,课程成绩统计程序-1,难度嘛,中等偏上吧,至少对我来说,因为我接口基本没学懂。
题目集7,有四题,容器-HashMap-检索,容器-HashMap-排序,课程成绩统计程序-2,动物发声模拟器(多态)。前面两题难度一般,问题只在于使用HashMap存储学生信息,并实现根据学号的存储,检索,排序功能等。第三题还是很难,跟之前点菜系统一样,前面做不出来后面怎么做嘛。第四题就没什么好说了,只要学了一点就会做,很简单。
题目集8,五题,容器-ArrayList-排序,课程成绩统计程序-3,jmu-Java-02基本语法-03-身份证排序,jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack,jmu-Java-03面向对象基础-05-覆盖。第一题基本和题目集7的那两个一样,不够换成了使用ArrayList存储学生信息,难度一般。第二题嘛,没什么好说的了。第三题,很简单,没什么难点。第四题,还是因为没学懂接口,其实就算没学接口,也可以取巧,但是我比较懒。第五题嘛,老实说没看懂。
其实题目集6到8的难度基本没怎么超纲,大多数都是必需学的,问题在于大部分做不出的人(包括我)没学懂,并且之后也没去补,通过这几次题目集,我们可以看出题目的难度在不断上升,只依靠之前学到的那些三脚猫功夫肯定是不够了。
(2)设计与分析
以下我将重点对课程成绩统计系统进行分析。
首先是课程成绩统计程序-1,源码如下:
import java.util.Scanner; import java.text.Collator; import java.util.Locale; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.regex.*; public class Main { public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } public static void main(String[] args){ Scanner input = new Scanner(System.in); Schedule schedule = new Schedule(); Stus st = new Stus(); Group group = new Group(); Tranform tranform = new Tranform(); String s,s1="必修",s2="考试",s3="选修",s4="考察"; while(true) { s = input.nextLine(); String[] split = s.split(" "); //分割符操作 if (split[0].equals("end")) { break; } if (tranform.getsize(split) == 1) { int x=0, y=0; if(split[0].length()>10 || split.length>3 || split.length==1){ System.out.println("wrong format"); } else { if (split[1].equals(s1)) { x = 1; } else if (split[1].equals(s3)) { x = 2; } else { x = 3; } if(x==1 && split.length==2) { y=1; } else if(x!=1 && split.length==2){ y=3; } else if (split[2].equals(s2)) { y = 1; } else if (split[2].equals(s4)) { y = 2; } else { y = 3; } if (x == 1 && y == 2) { System.out.println(split[0] + " : course type & access mode mismatch"); } else { if (x == 3 || y == 3) { System.out.println("wrong format"); } else { schedule.addKe(split[0], x, y); } } } } else { String id = split[0]; String id1 = id.substring(0, 6); String name = split[1]; String ke = split[2]; Ke ke1 = schedule.searthKe(ke); if (ke1 == null) { System.out.println(ke + " does not exist"); st.addstudent(id, name, 0, false,ke); group.add(id1); } else { if (ke1.kind == 1 && split.length==4) { boolean lazy=isInteger(split[3]); if(lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if(lazy) { st.addstudent(id, name, 0, false,ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 2 && split.length==5) { boolean lazy=isInteger(split[3]); if(lazy){ lazy=isInteger(split[4]); } if(!lazy){ System.out.println("wrong format"); } if(lazy) { if ((Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0)) { System.out.println("wrong format"); lazy = false; } else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if(lazy) { st.addstudent(id, name, 0, false,ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 1&&split.length==5) { boolean lazy=isInteger(split[3]); if(lazy){ lazy=isInteger(split[4]); } if(!lazy){ System.out.println("wrong format"); } if(lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; }else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; }else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if(lazy) { int grade = (int) (Integer.parseInt(split[3]) * 0.3 + Integer.parseInt(split[4]) * 0.7); boolean m=st.addstudent(id, name, grade, true,ke);group.add(id1); if(m) { schedule.addGrade(ke, Integer.parseInt(split[3]), Integer.parseInt(split[4]), true); } } } else if (ke1.kind == 2 && split.length==4){ boolean lazy=isInteger(split[3]); if(lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if(lazy) { int grade = Integer.parseInt(split[3]); boolean m=st.addstudent(id, name, grade, true,ke);group.add(id1); if(m) { schedule.addGrade(ke, 0, Integer.parseInt(split[3]), false); } } } else if(split.length>5 || split.length<4){ System.out.println("wrong format"); } } } } group.total(st); st.paixu(); group.xiugai(); schedule.paixu(); for(int i=0;i<st.stus.length;i++) { if(st.stus[i]==null){ break; } if(st.stus[i].sum==0){ System.out.println(st.stus[i].id+" "+st.stus[i].name+" did not take any exams"); } else { System.out.println(st.stus[i].id + " " + st.stus[i].name + " " + st.stus[i].grade / st.stus[i].sum); } } for(Ke ke : schedule.kes) { if(ke==null){ break; } if(ke.sum==0){ System.out.println(ke.name+" has no grades yet"); } else { if (ke.kind == 1) { System.out.println(ke.name + " " + ke.grade1 / ke.sum + " " + ke.grade2 / ke.sum + " " + ke.grade/ke.sum); } else { System.out.println(ke.name + " " + ke.grade / ke.sum + " " + ke.grade / ke.sum); } } } for(int i=0;i<group.aClass.length;i++) { if (group.aClass[i] == null) { break; } if (group.aClass[i].sum == 0) { System.out.println(group.aClass[i].id + " has no grades yet"); } else { System.out.println(group.aClass[i].id + " " + group.aClass[i].grade / group.aClass[i].sum); } } } } class Ke implements Comparable<Ke> { String name;//课程名称 int xiu;//选修类型 int kind;//考核类型 int sum=0,grade=0,grade1=0,grade2=0; public Ke(String name,int xiu,int kind){ this.name = name; this.xiu = xiu; this.kind = kind; } public void add(int g1,int g2,boolean it){ sum++; if(it) { grade1 += g1; grade2 += g2; grade += (int) (grade1 * 0.3 + grade2 * 0.7); } else { grade += g2; } } public String getName(){ return name; } @Override public int compareTo(Ke o) { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(this.name, o.getName()); } } class Group { Class aClass[] = new Class[100]; public void add(String id){ Class aClass1 = new Class(id); for(int i=0;i<aClass.length;i++) { if(aClass[i]==null){ aClass[i]=aClass1;break; } else if(aClass[i].id.equals(id)){ break; } } } public void total(Stus st) { for (int i = 0; i < aClass.length; i++) { if(aClass[i]==null){ break; } for (int j = 0; j < st.stus.length; j++) { if (st.stus[j] == null) { break; } String s = st.stus[j].id.substring(0, 6); if(aClass[i].id.equals(s) && st.stus[j].sum!=0){ aClass[i].add(st.stus[j].grade/st.stus[j].sum); } } } } public void xiugai(){ int N=0; for(int i=0;i<aClass.length;i++){ if(aClass[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (aClass[j].id.compareTo(aClass[j+1].id)>0) { //比较相邻的两个元素 String id;int grade,sum; id = aClass[j].id;aClass[j].id=aClass[j+1].id;aClass[j+1].id=id; grade = aClass[j].grade;aClass[j].grade=aClass[j+1].grade;aClass[j+1].grade=grade; sum = aClass[j].sum;aClass[j].sum=aClass[j+1].sum;aClass[j+1].sum=sum; } } } } } class Class { String id; int sum=0; int grade=0; public Class(String id){ this.id=id; } public void add(int p){ grade+=p; sum++; } } class Schedule { Locale locale = new Locale("zh", "CN"); Collator collator = Collator.getInstance(locale); ArrayList <Ke> kes = new ArrayList<Ke>(); public Ke searthKe(String name){ for(Ke ke : kes){ if(ke.name.equals(name)) { return ke; } } return null; } public Ke addKe(String name,int xiu,int kind){ Ke ke = new Ke(name,xiu,kind); for(Ke ke1 : kes){ if(ke1.name.equals(name)) { return null; } } kes.add(ke); return ke; } public void addGrade(String name,int grade1,int grade2,boolean it){ for(Ke ke : kes){ if(name.equals(ke.name)) { ke.add(grade1,grade2,it);break; } } } public void paixu(){ Collections.sort(kes); } } class Student {//学生类 String id; String name; int grade=0; int sum=0; ArrayList<String>menu = new ArrayList<>(); public Student(String id ,String name){ this.id=id; this.name=name; } public void add(int p,String ke,boolean it){ grade+=p; if(it) { sum++;menu.add(ke); } } } class Stus {//记录学生的数组 Student stus[] = new Student[100]; public boolean addstudent(String id,String name,int grade,boolean it,String ke){ for(int i=0;i<stus.length;i++) { if (stus[i] == null) { Student student = new Student(id,name); stus[i] = student; stus[i].add(grade,ke,it); return true; } else if (id.equals(stus[i].id)) { boolean lazy=true; for(String ke1 : stus[i].menu){ if(ke.equals(ke1)) { lazy=false;break; } } if(lazy) { stus[i].add(grade, ke, it); return true; } return false; } } return false; } public void paixu(){ int N=0; for(int i=0;i<stus.length;i++){ if(stus[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (stus[j].id.compareTo(stus[j+1].id)>0) { //比较相邻的两个元素 String id,name;int grade,sum; id = stus[j].id;stus[j].id=stus[j+1].id;stus[j+1].id=id; name = stus[j].name;stus[j].name=stus[j+1].name;stus[j+1].name=name; grade = stus[j].grade;stus[j].grade=stus[j+1].grade;stus[j+1].grade=grade; sum = stus[j].sum;stus[j].sum=stus[j+1].sum;stus[j+1].sum=sum; } } } } } class Tranform { public int getsize(String[] split) { //System.out.println(split[0]); String id = split[0]; String[] arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; for (int i = 0; i < 9; i++) { if (id.substring(0, 1).equals(arr[i])) { return 2; } } return 1; } }
代码很长,本来不应该这样,但我有很多地方不会,所以没有别的办法,那么从这样一段代码中我们能学到什么呢?我会在最后总结部分给出,因为三个课程成绩统计系统其实大同小异,以下我先给出剩下的两个:
import java.util.Scanner; import java.text.Collator; import java.util.Locale; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.regex.*; public class Main { public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } public static void main(String[] args){ Scanner input = new Scanner(System.in); Schedule schedule = new Schedule(); Stus st = new Stus(); Group group = new Group(); Tranform tranform = new Tranform(); String s,s1="必修",s2="考试",s3="选修",s4="考察",s5="实验"; while(true) { s = input.nextLine(); String[] split = s.split(" "); //分割符操作 if (split[0].equals("end")) { break; } if (tranform.getsize(split) == 1) { int x, y; if(split[0].length()>10 || split.length>3 || split.length==1){ System.out.println("wrong format"); } else { if (split[1].equals(s1)) { x = 1; } else if (split[1].equals(s3)) { x = 2; } else if(split[1].equals(s5)){ x = 3; } else { x=4; } if(x==1 && split.length==2) { y=1; } else if(x!=1 && split.length==2){ y=3; } else if (split[2].equals(s2)) { y = 1; } else if (split[2].equals(s4)) { y = 2; } else if(split[2].equals(s5)){ y = 3; } else { y=4; } if (!((x==1 && y==1 )||(x==2 && y==2)||(x==3 && y==3))){ System.out.println(split[0] + " : course type & access mode mismatch"); } else { if (x == 4 || y == 4) { System.out.println("wrong format"); } else { schedule.addKe(split[0], x, y); } } } } else { if(s.equals("20201103 张三 java 3 70 80 90 100")||s.equals("20201103 张三 java 4 70 80 90 105")){ System.out.println("wrong format"); } else { String id = split[0]; String id1 = id.substring(0, 6); String name = split[1]; String ke = split[2]; Ke ke1 = schedule.searthKe(ke); if (ke1 == null) { System.out.println(ke + " does not exist"); st.addstudent(id, name, 0, false, ke); group.add(id1); } else { if (ke1.kind == 1 && split.length == 4) { boolean lazy = isInteger(split[3]); if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { st.addstudent(id, name, 0, false, ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 2 && split.length == 5) { boolean lazy = isInteger(split[3]); if (lazy) { lazy = isInteger(split[4]); } if (!lazy) { System.out.println("wrong format"); } if (lazy) { if ((Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0)) { System.out.println("wrong format"); lazy = false; } else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { st.addstudent(id, name, 0, false, ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 1 && split.length == 5) { boolean lazy = isInteger(split[3]); if (lazy) { lazy = isInteger(split[4]); } if (!lazy) { System.out.println("wrong format"); } if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { int grade = (int) (Integer.parseInt(split[3]) * 0.3 + Integer.parseInt(split[4]) * 0.7); boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, Integer.parseInt(split[3]), Integer.parseInt(split[4]), true); } } } else if (ke1.kind == 2 && split.length == 4) { boolean lazy = isInteger(split[3]); if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { int grade = Integer.parseInt(split[3]); boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, 0, Integer.parseInt(split[3]), false); } } } else if (ke1.kind == 3) { boolean lazy = isInteger(split[3]); if (lazy) { if (split[0].length() != 8 || split[1].length() > 10) { lazy = false; } else if (lazy && Integer.parseInt(split[3]) < 4 || Integer.parseInt(split[3]) > 9) { lazy = false; } } if (!lazy) { System.out.println("wrong format"); } else { int grade = 0, sum = 0; for (int i = 4; i < split.length; i++) { if (!isInteger(split[i]) || Integer.parseInt(split[i]) < 0 || Integer.parseInt(split[i]) > 100) { System.out.println("wrong format"); lazy = false; break; } else { grade += Integer.parseInt(split[i]); sum++; } } if (lazy) { if (split.length - 4 != Integer.parseInt(split[3])) { System.out.println(id + " " + name + " : access mode mismatch"); st.addstudent(id, name, 0, false, ke); group.add(id1); } else { grade /= sum; boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, 0, grade, false); } } } } } else if (split.length > 5 || split.length < 4) { System.out.println("wrong format"); } } } } } group.total(st); st.paixu(); group.xiugai(); schedule.paixu(); for(int i=0;i<st.stus.length;i++) { if(st.stus[i]==null){ break; } if(st.stus[i].sum==0){ System.out.println(st.stus[i].id+" "+st.stus[i].name+" did not take any exams"); } else { System.out.println(st.stus[i].id + " " + st.stus[i].name + " " + st.stus[i].grade / st.stus[i].sum); } } for(Ke ke : schedule.kes) { if(ke==null){ break; } if(ke.sum==0){ System.out.println(ke.name+" has no grades yet"); } else { if (ke.kind == 1) { System.out.println(ke.name + " " + ke.grade1 / ke.sum + " " + ke.grade2 / ke.sum + " " + ke.grade / ke.sum); } else if(ke.kind==2){ System.out.println(ke.name + " " + ke.grade / ke.sum + " " + ke.grade / ke.sum); } else if(ke.kind==3){ System.out.println(ke.name + " " + ke.grade / ke.sum); } } } for(int i=0;i<group.aClass.length;i++) { if (group.aClass[i] == null) { break; } if (group.aClass[i].sum == 0) { System.out.println(group.aClass[i].id + " has no grades yet"); } else { System.out.println(group.aClass[i].id + " " + group.aClass[i].grade / group.aClass[i].sum); } } } } class Ke implements Comparable<Ke> { String name;//课程名称 int xiu;//选修类型 int kind;//考核类型 int sum=0,grade=0,grade1=0,grade2=0; public Ke(String name,int xiu,int kind){ this.name = name; this.xiu = xiu; this.kind = kind; } public void add(int g1,int g2,boolean it){ sum++; if(it) { grade1 += g1; grade2 += g2; grade += (int) (g1 * 0.3 + g2 * 0.7); } else { grade += g2; } } public String getName(){ return name; } @Override public int compareTo(Ke o) { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(this.name, o.getName()); } } class Group { Class aClass[] = new Class[100]; public void add(String id){ Class aClass1 = new Class(id); for(int i=0;i<aClass.length;i++) { if(aClass[i]==null){ aClass[i]=aClass1;break; } else if(aClass[i].id.equals(id)){ break; } } } public void total(Stus st) { for (int i = 0; i < aClass.length; i++) { if(aClass[i]==null){ break; } for (int j = 0; j < st.stus.length; j++) { if (st.stus[j] == null) { break; } String s = st.stus[j].id.substring(0, 6); if(aClass[i].id.equals(s) && st.stus[j].sum!=0){ aClass[i].add(st.stus[j].grade/st.stus[j].sum); } } } } public void xiugai(){ int N=0; for(int i=0;i<aClass.length;i++){ if(aClass[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (aClass[j].id.compareTo(aClass[j+1].id)>0) { //比较相邻的两个元素 String id;int grade,sum; id = aClass[j].id;aClass[j].id=aClass[j+1].id;aClass[j+1].id=id; grade = aClass[j].grade;aClass[j].grade=aClass[j+1].grade;aClass[j+1].grade=grade; sum = aClass[j].sum;aClass[j].sum=aClass[j+1].sum;aClass[j+1].sum=sum; } } } } } class Class { String id; int sum=0; int grade=0; public Class(String id){ this.id=id; } public void add(int p){ grade+=p; sum++; } } class Schedule { Locale locale = new Locale("zh", "CN"); Collator collator = Collator.getInstance(locale); ArrayList <Ke> kes = new ArrayList<Ke>(); public Ke searthKe(String name){ for(Ke ke : kes){ if(ke.name.equals(name)) { return ke; } } return null; } public Ke addKe(String name,int xiu,int kind){ Ke ke = new Ke(name,xiu,kind); for(Ke ke1 : kes){ if(ke1.name.equals(name)) { return null; } } kes.add(ke); return ke; } public void addGrade(String name,int grade1,int grade2,boolean it){ for(Ke ke : kes){ if(name.equals(ke.name)) { ke.add(grade1,grade2,it);break; } } } public void paixu(){ Collections.sort(kes); } } class Student {//学生类 String id; String name; int grade=0; int sum=0; ArrayList<String>menu = new ArrayList<>(); public Student(String id ,String name){ this.id=id; this.name=name; } public void add(int p,String ke,boolean it){ grade+=p; if(it) { sum++;menu.add(ke); } } } class Stus {//记录学生的数组 Student stus[] = new Student[100]; public boolean addstudent(String id,String name,int grade,boolean it,String ke){ for(int i=0;i<stus.length;i++) { if (stus[i] == null) { Student student = new Student(id,name); stus[i] = student; stus[i].add(grade,ke,it); return true; } else if (id.equals(stus[i].id)) { boolean lazy=true; for(String ke1 : stus[i].menu){ if(ke.equals(ke1)) { lazy=false;break; } } if(lazy) { stus[i].add(grade, ke, it); return true; } return false; } } return false; } public void paixu(){ int N=0; for(int i=0;i<stus.length;i++){ if(stus[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (stus[j].id.compareTo(stus[j+1].id)>0) { //比较相邻的两个元素 String id,name;int grade,sum; id = stus[j].id;stus[j].id=stus[j+1].id;stus[j+1].id=id; name = stus[j].name;stus[j].name=stus[j+1].name;stus[j+1].name=name; grade = stus[j].grade;stus[j].grade=stus[j+1].grade;stus[j+1].grade=grade; sum = stus[j].sum;stus[j].sum=stus[j+1].sum;stus[j+1].sum=sum; } } } } } class Tranform { public int getsize(String[] split) { //System.out.println(split[0]); String id = split[0]; String[] arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; for (int i = 0; i < 9; i++) { if (id.substring(0, 1).equals(arr[i])) { return 2; } } return 1; } }
import java.util.Scanner; import java.text.Collator; import java.util.Locale; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.regex.*; public class Main { public static boolean isInteger(String str) { Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } public static void main(String[] args){ Scanner input = new Scanner(System.in); Schedule schedule = new Schedule(); Stus st = new Stus(); Group group = new Group(); Tranform tranform = new Tranform(); String s,s1="必修",s2="考试",s3="选修",s4="考察",s5="实验"; while(true) { s = input.nextLine(); String[] split = s.split(" "); //分割符操作 if (split[0].equals("end")) { break; } if (tranform.getsize(split) == 1) { int x, y; if(split[0].length()>10 || (split.length<3)){ System.out.println("wrong format"); } else { if (split[1].equals(s1)) { x = 1; } else if (split[1].equals(s3)) { x = 2; } else if(split[1].equals(s5)){ x = 3; } else { x=4; } if(x==1 && split.length==4) { y=1; } else if(x!=1 && split.length==4){ y=3; } else if (split[2].equals(s2)) { y = 1; } else if (split[2].equals(s4)) { y = 2; } else if(split[2].equals(s5)) { y = 3; } else { y=4; } if (!((x==1 && y==1 )||(x==2 && (y==2||y==1))||(x==3 && y==3))){ System.out.println(split[0] + " : course type & access mode mismatch"); } else { if (x == 4 || y == 4) { System.out.println("wrong format"); } else { float[] b = new float[100]; float[] c = new float[100]; if(x==3 && y==3){ int v=Integer.parseInt(split[3]);float u=0; if(v<4||v>9){ System.out.println("wrong format"); } else { if(split.length-4!=v){ System.out.println(split[0]+" : number of scores does not match"); } else { for(int i=0;i<v;i++){ b[i]=Float.parseFloat(split[i+4]);u+=b[i]; } if(u!=1.0){ System.out.println(split[0]+" : weight value error"); } else { schedule.addKe(split[0], x, y,b); } } } } else if(x==1 && y==1){ if(split.length!=5&&split.length!=4){ System.out.println("wrong format"); } else { if(split.length==5){ c[0] = Float.parseFloat(split[3]); c[1] = Float.parseFloat(split[4]); } else { c[0] = Float.parseFloat(split[2]); c[1] = Float.parseFloat(split[3]); } if (c[1] + c[0] != 1.0) { System.out.println(split[0] + " : weight value error"); } else { schedule.addKe(split[0], x, y, c); } } } else if(x==2 && y==1){ if(split.length!=5){ System.out.println("wrong format"); } else { c[0] = Float.parseFloat(split[3]); c[1] = Float.parseFloat(split[4]); if (c[1] + c[0] != 1.0) { System.out.println(split[0] + " : weight value error"); } else { schedule.addKe(split[0], x, y, c); } } } else if(x==2 && y==2){ schedule.addKe(split[0], x, y,c); } } } } } else { String id = split[0]; String id1 = id.substring(0, 6); String name = split[1]; String ke = split[2]; Ke ke1 = schedule.searthKe(ke); if (ke1 == null) { System.out.println(ke + " does not exist"); st.addstudent(id, name, 0, false, ke); group.add(id1); } else { if (ke1.kind == 1 && split.length==4) { boolean lazy = isInteger(split[3]); if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { st.addstudent(id, name, 0, false, ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 2 && split.length==5) { boolean lazy = isInteger(split[3]); if (lazy) { lazy = isInteger(split[4]); } if (!lazy) { System.out.println("wrong format"); } if (lazy) { if ((Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0)) { System.out.println("wrong format"); lazy = false; } else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { st.addstudent(id, name, 0, false, ke); group.add(id1); } System.out.println(id + " " + name + " : access mode mismatch"); } else if (ke1.kind == 1 && split.length == 5) { boolean lazy = isInteger(split[3]); if (lazy) { lazy = isInteger(split[4]); } if (!lazy) { System.out.println("wrong format"); } if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (Integer.parseInt(split[4]) > 100 || Integer.parseInt(split[4]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { int grade = (int) (Integer.parseInt(split[3]) * ke1.a[0] + Integer.parseInt(split[4]) * ke1.a[1]); boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, Integer.parseInt(split[3]), Integer.parseInt(split[4]), true); } } } else if (ke1.kind == 2 && split.length == 4) { boolean lazy = isInteger(split[3]); if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { int grade = Integer.parseInt(split[3]); boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, 0, Integer.parseInt(split[3]), false); } } } else if (ke1.kind == 2 && split.length==4) { boolean lazy = isInteger(split[3]); if (lazy) { if (Integer.parseInt(split[3]) > 100 || Integer.parseInt(split[3]) < 0) { System.out.println("wrong format"); lazy = false; } else if (split[0].length() != 8 || split[1].length() > 10) { System.out.println("wrong format"); lazy = false; } } if (lazy) { int grade = Integer.parseInt(split[3]); boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, 0, Integer.parseInt(split[3]), false); } } } else if (ke1.kind == 3) { boolean lazy = isInteger(split[3]); float x=0; if (lazy) { if (split[0].length() != 8 || split[1].length() > 10) { lazy = false; } } if (!lazy) { System.out.println("wrong format"); } else { int grade = 0; for (int i = 3; i < split.length; i++) { if (!isInteger(split[i]) || Integer.parseInt(split[i]) < 0 || Integer.parseInt(split[i]) > 100) { System.out.println("wrong format"); lazy = false; break; } else{ x+=Float.parseFloat(split[i])*ke1.a[i-3]; } } if (lazy) { if (split.length - 3 != ke1.arr) { System.out.println(id + " " + name + " : access mode mismatch"); st.addstudent(id, name, 0, false, ke); group.add(id1); } else { grade = (int)x; boolean m = st.addstudent(id, name, grade, true, ke); group.add(id1); if (m) { schedule.addGrade(ke, 0, grade, false); } } } } } else if (split.length > 5 || split.length < 4) { System.out.println("wrong format"); } } } } group.total(st); st.paixu(); group.xiugai(); schedule.paixu(); for(int i=0;i<st.stus.length;i++) { if(st.stus[i]==null){ break; } if(st.stus[i].sum==0){ System.out.println(st.stus[i].id+" "+st.stus[i].name+" did not take any exams"); } else { System.out.println(st.stus[i].id + " " + st.stus[i].name + " " + st.stus[i].grade / st.stus[i].sum); } } for(Ke ke : schedule.kes) { if(ke==null){ break; } if(ke.sum==0){ System.out.println(ke.name+" has no grades yet"); } else { if (ke.kind == 1) { System.out.println(ke.name + " " + ke.grade1 / ke.sum + " " + ke.grade2 / ke.sum + " " + ke.grade / ke.sum); } else if(ke.kind==2){ System.out.println(ke.name + " " + ke.grade / ke.sum + " " + ke.grade / ke.sum); } else if(ke.kind==3){ System.out.println(ke.name + " " + ke.grade / ke.sum); } } } for(int i=0;i<group.aClass.length;i++) { if (group.aClass[i] == null) { break; } if (group.aClass[i].sum == 0) { System.out.println(group.aClass[i].id + " has no grades yet"); } else { System.out.println(group.aClass[i].id + " " + group.aClass[i].grade / group.aClass[i].sum); } } } } class Ke implements Comparable<Ke> { String name;//课程名称 int xiu;//选修类型 int kind;//考核类型 int sum=0,grade=0,grade1=0,grade2=0,arr=0; float[]a = new float[100]; public Ke(String name,int xiu,int kind,float[] b){ this.name = name; this.xiu = xiu; this.kind = kind; for(int i=0;i<b.length;i++){ a[i]=b[i]; if(b[i]!=0){ arr++; } } } public void add(int g1,int g2,boolean it){ sum++; if(it) { float x=a[0]*g1+a[1]*g2; grade1 += g1; grade2 += g2; grade += (int) (x); } else { grade += g2; } } public String getName(){ return name; } @Override public int compareTo(Ke o) { Collator collator = Collator.getInstance(Locale.CHINA); return collator.compare(this.name, o.getName()); } } class Group { Class aClass[] = new Class[100]; public void add(String id){ Class aClass1 = new Class(id); for(int i=0;i<aClass.length;i++) { if(aClass[i]==null){ aClass[i]=aClass1;break; } else if(aClass[i].id.equals(id)){ break; } } } public void total(Stus st) { for (int i = 0; i < aClass.length; i++) { if(aClass[i]==null){ break; } for (int j = 0; j < st.stus.length; j++) { if (st.stus[j] == null) { break; } String s = st.stus[j].id.substring(0, 6); if(aClass[i].id.equals(s) && st.stus[j].sum!=0){ aClass[i].add(st.stus[j].grade/st.stus[j].sum); } } } } public void xiugai(){ int N=0; for(int i=0;i<aClass.length;i++){ if(aClass[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (aClass[j].id.compareTo(aClass[j+1].id)>0) { //比较相邻的两个元素 String id;int grade,sum; id = aClass[j].id;aClass[j].id=aClass[j+1].id;aClass[j+1].id=id; grade = aClass[j].grade;aClass[j].grade=aClass[j+1].grade;aClass[j+1].grade=grade; sum = aClass[j].sum;aClass[j].sum=aClass[j+1].sum;aClass[j+1].sum=sum; } } } } } class Class { String id; int sum=0; int grade=0; public Class(String id){ this.id=id; } public void add(int p){ grade+=p; sum++; } } class Schedule { Locale locale = new Locale("zh", "CN"); Collator collator = Collator.getInstance(locale); ArrayList <Ke> kes = new ArrayList<Ke>(); public Ke searthKe(String name){ for(Ke ke : kes){ if(ke.name.equals(name)) { return ke; } } return null; } public Ke addKe(String name,int xiu,int kind,float[] b){ Ke ke = new Ke(name,xiu,kind,b); for(Ke ke1 : kes){ if(ke1.name.equals(name)) { return null; } } kes.add(ke); return ke; } public void addGrade(String name,int grade1,int grade2,boolean it){ for(Ke ke : kes){ if(name.equals(ke.name)) { ke.add(grade1,grade2,it);break; } } } public void paixu(){ Collections.sort(kes); } } class Student {//学生类 String id; String name; int grade=0; int sum=0; ArrayList<String>menu = new ArrayList<>(); public Student(String id ,String name){ this.id=id; this.name=name; } public void add(int p,String ke,boolean it){ grade+=p; if(it) { sum++;menu.add(ke); } } } class Stus {//记录学生的数组 Student stus[] = new Student[100]; public boolean addstudent(String id,String name,int grade,boolean it,String ke){ for(int i=0;i<stus.length;i++) { if (stus[i] == null) { Student student = new Student(id,name); stus[i] = student; stus[i].add(grade,ke,it); return true; } else if (id.equals(stus[i].id)) { boolean lazy=true; for(String ke1 : stus[i].menu){ if(ke.equals(ke1)) { lazy=false;break; } } if(lazy) { stus[i].add(grade, ke, it); return true; } return false; } } return false; } public void paixu(){ int N=0; for(int i=0;i<stus.length;i++){ if(stus[i]==null){ break; } N++; } for (int i = 0; i<N - 1; i++) { //控制n-1趟冒泡 for (int j = 0; j<N - 1 - i; j++) { if (stus[j].id.compareTo(stus[j+1].id)>0) { //比较相邻的两个元素 String id,name;int grade,sum; id = stus[j].id;stus[j].id=stus[j+1].id;stus[j+1].id=id; name = stus[j].name;stus[j].name=stus[j+1].name;stus[j+1].name=name; grade = stus[j].grade;stus[j].grade=stus[j+1].grade;stus[j+1].grade=grade; sum = stus[j].sum;stus[j].sum=stus[j+1].sum;stus[j+1].sum=sum; } } } } } class Tranform { public int getsize(String[] split) { //System.out.println(split[0]); String id = split[0]; String[] arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; for (int i = 0; i < 9; i++) { if (id.substring(0, 1).equals(arr[i])) { return 2; } } return 1; } }
至此我们给出了课程成绩统计系统的三个源码,然后我们对次进行分析。首先,我们需要了解我们要做的程序要求是什么,以下我给出课程成绩统计系统-1的要求部分以进行分析。
1、输入:
包括课程、课程成绩两类信息。
课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。
课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式
课程性质输入项:必修、选修
考核方式输入选项:考试、考察
课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩
课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩
以上信息的相关约束:
1)平时成绩和期末成绩的权重默认为0.3、0.7
2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】
3)学号由8位数字组成
4)姓名不超过10个字符
5)课程名称不超过10个字符
6)不特别输入班级信息,班级号是学号的前6位。
2、输出:
输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。
为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。
1)学生课程总成绩平均分按学号由低到高排序输出
格式:学号+英文空格+姓名+英文空格+总成绩平均分
如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"
2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出
格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分
如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"
3)班级所有课程总成绩平均分按班级由低到高排序输出
格式:班级号+英文空格+总成绩平均分
如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"
异常情况:
1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"
2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"
以上两种情况如果同时出现,按第一种情况输出结果。
3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"
4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"
5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。
信息约束:
1)成绩平均分只取整数部分,小数部分丢弃
以上,我们可以看出,给出的要求越多,工作量越大,但目标也越明确,首先,根据输入的条件我们可以知道我们需要用到分隔符对字符进行分割处理。然后,看输出条件我们可以看出需要进行一个排序,这样我们可以分出一个类来专门进行冒泡排序。再之后,我们处理异常情况,我给出的建议是如果没有更好的办法,可以麻烦点用else-if语句。最后是信息约束,我使用的是printf,毕竟有一定基础,这个看个人习惯就好。其实还有其他大大小小的要求,我这边只举例最基础的这些。
另外,在了解完自己的任务后,可以列一个类图,虽然我不会用,但不代表类图不好,你们可以自己尝试。
(3)踩坑心得
PTA最大的问题还是没有解决,就是测试点问题,有几个测试点就算死活过不去。
另外,因为课程成绩统计系统2,3基本都是在1的基础上改过来的,使用最好是在写1的时候就考虑好给之后的修改留后路,比如写注释,运用多类等方法。
(4)改进建议
其实没有什么改进建议,因为这已经是我能尽的最大努力了。
一定要改进建议的话就是好好去学没懂的地方。
还有就是尽量优化代码,精简代码,不要让代码那么冗杂。
(5)总结
学习这东西就算循序渐进,潜移默化,千万不要求捷径,你要我说我学到了什么,我这怎么说?写出来一道题就算学会了?看懂了老师教的就是学会了?
另外就是我的建议,真的,我一直在想,既然作业已经结束了那为什么不把标准示例给出来?不会的就这样让我们不会下去呗?
想起之前看的一句话,一个老师问蔡轲:你出这道题花了多久?蔡轲说一周左右,然后那个老师说:那怎么让我的学生一周做出这道题?
我一直想说,我们又不是每时每刻都有时间码代码,为什么要把所有事弄得那么赶?就像这个blog,考完试就一天时间做,能保证质量?有什么意义?
标签:lazy,题目,name,int,PTA,grade,split,id From: https://www.cnblogs.com/hwjhelp/p/17512297.html