第一次javablog大作业
1.前言:第一次大作业主要是让我们熟悉对类和对象的使用和理解,更加熟悉方法的使用以及对private的使用和理解,加强我们对输入输出以及查询等基本功能的使用,还让我们对字符串的处理更加熟悉,了解到length,substring,compareTo等方法的使用,熟悉数组的使用,虽然当时还是第一次接触面对对象的处理方法,但幸好题目难度不是很大,题目有五道题,稍微有难度的也就是最后一题,所以才能侥幸拿到满分吧。然而第二次大作业的难度明显有了很大的提升,虽然题目量由之前的五道题变成了四道题,但完成第二次大作业的时间明显的比完成第一次大作业的时间长了不少。第二次大作业的最后一题是在第一次大作业的最后一题的基础上添加了试题分数和试卷功能,让我们在上一题的基础上添加一些功能,体会一下面向对象语言和面向过程语言的不同之处,增加的试卷功能需要我们再添加一个试卷类,将题目和分值录入,第二次大作业没有给我们标明有多少题目,所以如果继续用数组的话将会变得非常的麻烦,所以我认为第二次大作业还在考验我们对Arraylist的使用,集合将当于一个可变长的数组。然后第三次大作业的题目量再次减少了一道题,但是他的第二题的难度就已经感觉比第一次大作业的最后一题还要难,所以花的时间更长,第三次大作业的最后一道题比第二次大作业增加了很多错误的判断,比如输入格式,题目不存在,题目有问题之类的判断,还增加了答卷,所以这就需要我们在原来的基础上再增加一个答卷类,这次的大作业确实比前两次的难度都大了很多,即使我一直都在努力写也没有拿到满分,第三次大作业更加注重对数据的输入和输出的是否合法有效的判断上,这也是我们以后在开发软件上不可或缺的一个能力。
2.设计与分析:
(1),第一次大作业
对于第一次大作业我想就最后一题来分析一些我的对于这道题目的看法,首先我设计了一个题目questio类,用于存储每一道题目的内容和标准答案还有题号,再在类中写了一个判断输入答案是否与标准答案是否一样的方法,再用一个question的数组用来存储每一道题目,这样就构成了一个题目集,然后再设计了一个答案test类,用于存储每一张试卷的所有题目,由于答案的输入顺序是和题号的顺序相同,所以我在test里面写了一个按照题号冒泡排序的算法让输入的题目在题目集中按题号从小到大的顺序来排列,这样就可以顺利的和答案给的顺序对上号了,最后在main中输入和输出数据,因为当时对面对对象和面对过程有点搞不懂,所以我这次的大作业还是将很多东西都放在了main中,这样我的代码的复用性就不是很高,下次如果要加点东西就需要我修改大部分的代码了,对于输入数据,我是先对每一行的数据做完处理,然后判断他是题目还是答案,做完所有的处理,再将标准的答案与输入答案做对比,是否相等,这里有一个需要注意的细节,那就是空格问题,每一个部分都需要将空格去除,所有我们可以用trim的方法来对字符串做处理,最后再根据输出答案的格式将输出结果输出。
点击查看代码
import java.util.*;
class Question{
private String num;
private String neirong;
private String Standanswer;
public Question(){}
public Question(String num,String neirong,String Standanswer){
this.num=num;
this.neirong=neirong;
this.Standanswer=Standanswer;
}
public String getNum(){
return this.num.trim();
}
public String getNeirong(){
return this.neirong.trim();
}
public String getStandanswer()
{
return this.Standanswer.trim();
}
public boolean panduan(String a){
return this.Standanswer.trim().equals(a.trim());
}
}
class Test{
private int number;
private Question[] question;
private int i;
public Test(int num) {
this.number=num;
this.i=0;
question=new Question[number];
}
public void setNum(int c){
this.number=c;
}
public void setQuestion(Question b){
this.question[i]=b;
i++;
}
public Question getQuestion(int k){
return this.question[k];
}
public boolean pan(String as,Question c,String ss){
if(c.getNum()==as.trim()){
if(c.panduan(ss))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public void sun(){
for (int i = 0; i < number - 1; i++) {
for (int j = 0; j < number - 1 - i; j++) {
if (this.question[j].getNum().length()>this.question[j+1].getNum().length()) {
Question temp = this.question[j+1];
this.question[j+1] = this.question[j];
this.question[j] = temp;
}
else if ((this.question[j].getNum().compareTo(this.question[j+1].getNum())>0)&&(this.question[j].getNum().length()==this.question[j+1].getNum().length())) {
Question temp = this.question[j+1];
this.question[j+1] = this.question[j];
this.question[j] = temp;
}
}
}
}
}
public class Main{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int num=input.nextInt();
Test test=new Test(num);
test.setNum(num);
for(int i=0;i<num;i++){
String ad=input.next();
ad+=input.nextLine();
String q="";
String m="";
String w="";
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
q=ad.substring(3,j-1);
break;
}
}
int l=0;
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q')
{
l=j+3;
}
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='A'){
m=ad.substring(l,j-1);
w=ad.substring(j+3);
}
}
Question ques=new Question(q,m,w);
test.setQuestion(ques);
}
String qe=input.next();
qe+=input.nextLine();
String[] answer=new String[num];
int kk=0;
int[] af=new int[num];
for(int i=0;i<qe.length();i++){
if(qe.charAt(i)=='#'&&qe.charAt(i+1)=='A'){
af[kk]=i;
kk++;
}
}
int pli=0;
for(int l=0;l<kk;l++){
if(l==kk-1){
answer[pli]=qe.substring(af[l]+3);
pli++;
}
else{
int io=af[l]+3;
int op=af[l+1]-1;
answer[pli]=qe.substring(io,op);
pli++;
}
}
test.sun();
for(int i=0;i<num;i++){
System.out.println(test.getQuestion(i).getNeirong()+"~"+answer[i].trim());
}
for(int kj=0;kj<num;kj++){
if(kj!=num-1)
{
System.out.print(test.getQuestion(kj).panduan(answer[kj])+" ");
}
else
{
System.out.print(test.getQuestion(kj).panduan(answer[kj]));
}
}
input.close();
}
}
点击查看代码
import java.util.*;
class Question{
private String num;
private String neirong;
private String Standanswer;
public Question(){}
public Question(String num,String neirong,String Standanswer){
this.num=num;
this.neirong=neirong;
this.Standanswer=Standanswer;
}
public String getNum(){
return this.num.trim();
}
public String getNeirong(){
return this.neirong.trim();
}
public String getStandanswer()
{
return this.Standanswer.trim();
}
public boolean panduan(String a){
return this.Standanswer.trim().equals(a.trim());
}
}
class Test{
private String bianhao;
private List<Integer> score = new ArrayList<Integer>();
private List<Question> question = new ArrayList<Question>();
public Test(){
}
public void setQuestion(Question b){
question.add(b);
}
public void setScore(int score){
this.score.add(score);
}
public int getScore(int k){
return score.get(k);
}
public void setBianhao(String bianhao){
this.bianhao=bianhao;
}
public String getBianhao(){
return this.bianhao;
}
public Question getQuestion(int k){
return this.question.get(k);
}
public int getSize(){
return question.size();
}
}
class Answer{
private String bianhao;
private List<String> answer = new ArrayList<String>();
public String getBianhao(){
return this.bianhao;
}
public void setBianhao(String bianhao){
this.bianhao=bianhao;
}
public void reserve(String a){
answer.add(a);
}
public String getAnswer(int k){
return answer.get(k);
}
public int getsize(){
return answer.size();
}
}
public class Main{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
Answer[]answer=new Answer[100];
Test[]test=new Test[100];
List<Question> questions = new ArrayList<Question>();
int r=0;
int io=0;
String ad=input.nextLine();
while(ad.compareTo("end")!=0){
String q="";
String m="";
String w="";
if(ad.charAt(1)=='N'){
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
q=ad.substring(3,j-1);
break;
}
}
int l=0;
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
l=j+3;
}
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='A'){
m=ad.substring(l,j-1);
w=ad.substring(j+3);
}
}
Question ques=new Question(q,m,w);
questions.add(ques);
}
else if(ad.charAt(1)=='S'){
int kk=0;
int[] af=new int[100];
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)=='#'){
af[kk]=i;
kk++;
}
}
if(kk<=1){
answer[r]=new Answer();
answer[r].setBianhao(ad.substring(af[0]+3));
}
else{
answer[r]=new Answer();
answer[r].setBianhao(ad.substring(af[0]+3,af[1]-1));
for(int l=1;l<kk;l++){
if(l==kk-1){
answer[r].reserve(ad.substring(af[l]+3));
}
else{
int iii=af[l]+3;
int op=af[l+1]-1;
answer[r].reserve(ad.substring(iii,op));
}
}
}
r++;
}
else if(ad.charAt(1)=='T'){
int kk=0;
int ll=0;
int[] af=new int[100];
int[] ag=new int[100];
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)==' '){
af[kk]=i;
kk++;
}
if(ad.charAt(i)=='-'){
ag[ll]=i;
ll++;
}
}
test[io]=new Test();
test[io].setBianhao(ad.substring(3,af[0]));
for(int i=0;i<ll;i++){
String aaa=ad.substring(af[i]+1,ag[i]);
String bbb;
if(i==ll-1){
bbb=ad.substring(ag[i]+1);
}
else{
bbb=ad.substring(ag[i]+1,af[i+1]);
}
for(int iw=0;iw<questions.size();iw++){
if(questions.get(iw).getNum().compareTo(aaa)==0){
test[io].setScore(Integer.parseInt(bbb));
test[io].setQuestion(questions.get(iw));
}
}
}
io++;
}
ad=input.nextLine();
}
for(int pq=0;pq<io;pq++){
int total=0;
for(int v=0;v<test[pq].getSize();v++){
total=total+test[pq].getScore(v);
}
if(total!=100){
System.out.println("alert: full score of test paper"+test[pq].getBianhao()+" is not 100 points");
}
}
for(int pp=0;pp<r;pp++){
int yy=0;
for(int pq=0;pq<io;pq++){
if(test[pq].getBianhao().compareTo(answer[pp].getBianhao())==0){
yy=1;
int total=0;
for(int kl=0;kl<answer[pp].getsize();kl++){
if(kl<test[pq].getSize())
System.out.println(test[pq].getQuestion(kl).getNeirong()+"~"+answer[pp].getAnswer(kl)+"~"+test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl)));
}
if(test[pq].getSize()>answer[pp].getsize()){
for(int ij=0;ij<test[pq].getSize()-answer[pp].getsize();ij++){
System.out.println("answer is null");
}
}
if(test[pq].getSize()>answer[pp].getsize()){
for(int kl=0;kl<answer[pp].getsize();kl++){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
for(int kl=0;kl<test[pq].getSize()-answer[pp].getsize();kl++){
if(kl!=test[pq].getSize()-answer[pp].getsize()-1){
System.out.print("0 ");
}
else{
System.out.print("0");
}
}
}
else if(test[pq].getSize()>answer[pp].getsize()){
for(int kl=0;kl<answer[pp].getsize();kl++){
if(kl!=answer[pp].getsize()-1){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
else{
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl));
}
else{
System.out.print("0");
}
}
}
}
else {
for(int kl=0;kl<test[pq].getSize();kl++){
if(kl!=test[pq].getSize()-1){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
else{
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl));
}
else{
System.out.print("0");
}
}
}
}
System.out.println("~"+total);
}
}
if(yy==0){
System.out.println("The test paper number does not exist");
}
}
input.close();
}
}
点击查看代码
import java.util.*;
class Question{
private String num;
private String neirong;
private String Standanswer;
private boolean is=true;
public Question(){}
public boolean getIs() {
return is;
}
public void setIs(boolean is) {
this.is = is;
}
public Question(String num, String neirong, String Standanswer){
this.num=num;
this.neirong=neirong;
this.Standanswer=Standanswer;
}
public String getNum(){
return this.num.trim();
}
public String getNeirong(){
return this.neirong.trim();
}
public String getStandanswer()
{
return this.Standanswer.trim();
}
public boolean panduan(String a){
return this.Standanswer.trim().equals(a.trim());
}
}
class Student{
private List<String> name=new ArrayList<>();
private List<String> studentID=new ArrayList<>();
public String getName(int i) {
return name.get(i);
}
public void setName(String name) {
this.name.add(name);
}
public String getStudentID(int i) {
return studentID.get(i);
}
public void setStudentID(String studentID) {
this.studentID.add(studentID);
}
public int getSize(){
return name.size();
}
}
class Test{
private String bianhao;
private List<Integer> score = new ArrayList<Integer>();
private List<Question> question = new ArrayList<Question>();
public void setQuestion(Question b){
question.add(b);
}
public void setScore(int score){
this.score.add(score);
}
public List getScore(){
return score;
}
public int getScore(int k){
return score.get(k);
}
public void setBianhao(String bianhao){
this.bianhao=bianhao;
}
public String getBianhao(){
return this.bianhao;
}
public Question getQuestion(int k){
return this.question.get(k);
}
public int getSize(){
return question.size();
}
public void getPaper(int io){
int total=0;
for(int v=0;v<this.question.size();v++){
total=total+this.score.get(v);
}
if(total!=100){
System.out.println("alert: full score of test paper"+this.bianhao+" is not 100 points");
}
}
}
class Answer{
private String bianhao;
private String studentID;
private List<String> answer = new ArrayList<String>();
private List<Integer> num=new ArrayList<>();
public int getNum(int k) {
return num.get(k);
}
public void setNum(int num) {
this.num.add(num);
}
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getBianhao(){
return this.bianhao;
}
public void setBianhao(String bianhao){
this.bianhao=bianhao;
}
public void reserve(String a){
answer.add(a);
}
public String getAnswer(int k){
return answer.get(k);
}
public int getsize(){
return answer.size();
}
public void outPaper(List<Test> a,Student b,List<Question> c){
int y=0;
for(int i=0;i<a.size();i++){
if(this.bianhao.compareTo(a.get(i).getBianhao())==0){
y=1;
int total=0;
for(int j=0;j<a.get(i).getSize();j++){
if(j<num.size()) {
int ll = 0;
for (int kj = 0; kj < c.size(); kj++) {
if (a.get(i).getQuestion(j).getNum().compareTo(c.get(kj).getNum()) == 0) {
ll = 1;
for (int k = 0; k < a.get(i).getSize(); k++) {
if (this.num.get(j) == k+1) {
if (a.get(i).getQuestion(k).getIs()) {
System.out.println(a.get(i).getQuestion(k).getNeirong() + "~" + this.answer.get(j) + "~" + a.get(i).getQuestion(k).panduan(this.answer.get(j)));
if (a.get(i).getQuestion(k).panduan(this.answer.get(j))) {
total += a.get(i).getScore(k);
} else {
a.get(i).getScore().set(k, 0);
}
}
}
}
if(j==num.size()-1) {
for (int k = 0; k < a.get(i).getSize(); k++) {
if (!a.get(i).getQuestion(k).getIs()) {
a.get(i).getScore().set(k, 0);
System.out.println("the question " + a.get(i).getQuestion(k).getNum() + " invalid~0");
}
}
}
}
}
if (ll == 0) {
System.out.println("non-existent question~0");
}
}
}
for(int sd=0;sd<a.get(i).getSize()-num.size();sd++){
System.out.println("answer is null");
a.get(i).getScore().set(num.size()+sd,0);
}
int lk=0;
for(int p=0;p<b.getSize();p++){
if(this.studentID.compareTo(b.getStudentID(p))==0){
lk=1;
System.out.print(b.getStudentID(p)+" "+b.getName(p)+":");
for(int j=0;j<a.get(i).getSize();j++){
//20201103 Tom: 0 0~0
System.out.print(" "+a.get(i).getScore(j));
if(j==a.get(i).getSize()-1)
System.out.println("~"+total);
}
}
}
if(lk==0) {
System.out.println(this.studentID + " not found");
}
}
}
if(y==0){
System.out.println("The test paper number does not exist");
}
}
}
class Tool{
public Test getTestmassage(String ad,List<Question> questions){
int kk=0;
int ll=0;
Test test=new Test();
int[] af=new int[100];
int[] ag=new int[100];
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)==' '){
af[kk]=i;
kk++;
}
if(ad.charAt(i)=='-'){
ag[ll]=i;
ll++;
}
}
test.setBianhao(ad.substring(3,af[0]));
for(int i=0;i<ll;i++){
String aaa=ad.substring(af[i]+1,ag[i]);
String bbb;
if(i==ll-1){
bbb=ad.substring(ag[i]+1);
}
else{
bbb=ad.substring(ag[i]+1,af[i+1]);
}
int lkj=0;
for(int iw=0;iw<questions.size();iw++){
if(questions.get(iw).getNum().compareTo(aaa)==0){
lkj=1;
test.setScore(Integer.parseInt(bbb));
test.setQuestion(questions.get(iw));
}
}
if(lkj==0){
Question asd=new Question("0","0","0");
test.setScore(0);
test.setQuestion(asd);
}
}
return test;
}
public Student getStudentmassage(String ad){
List<Integer> as=new ArrayList<>();
List<Integer> aq=new ArrayList<>();
for(int j=0;j<ad.length();j++) {
if (ad.charAt(j) == ' ') {
as.add(j);
}
if (ad.charAt(j) == '-') {
aq.add(j);
}
}
Student a=new Student();
for(int i=0;i<as.size();i++){
if(i==as.size()-1&&as.size()>1){
a.setStudentID(ad.substring(aq.get(i-1)+1,as.get(i)));
a.setName(ad.substring(as.get(i)+1));
}
else if(i==0&&i!=as.size()-1&&as.size()>1)
{
a.setStudentID(ad.substring(3,as.get(i)));
a.setName(ad.substring(as.get(i)+1,aq.get(i)));
}
else if(as.size()==1){
a.setStudentID(ad.substring(3,as.get(i)));
a.setName(ad.substring(as.get(i)+1));
}
else
{
a.setStudentID(ad.substring(aq.get(i-1)+1,as.get(i)));
a.setName(ad.substring(as.get(i)+1,aq.get(i)));
}
}
return a;
}
//#X:20201103 Tom-20201104 Jack-20201104 Www
public Question getQuestionmassage(String ad){
String q="";
String m="";
String w="";
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
q=ad.substring(3,j-1);
break;
}
}
int l=0;
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
l=j+3;
}
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='A'){
m=ad.substring(l,j-1);
w=ad.substring(j+3);
}
}
Question ques=new Question(q,m,w);
return ques;
}
public Answer getAnswermassage(String ad){
int kk=0;
int[] af=new int[100];
List<Integer> aq=new ArrayList<>();
List<Integer> aa=new ArrayList<>();
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)=='#'){
af[kk]=i;
kk++;
}
if(ad.charAt(i)=='-'){
aq.add(i);
}
if(aa.size()<=2) {
if (ad.charAt(i) == ' ') {
aa.add(i);
}
}
}
Answer answer=new Answer();
if(aa.size()>1) {
answer.setStudentID(ad.substring(aa.get(0) + 1, aa.get(1)));
}else{
answer.setStudentID(ad.substring(aa.get(0) + 1));
}
if(aa.size()<=1){
answer.setBianhao(ad.substring(3,aa.get(0)));
}
else{
answer.setBianhao(ad.substring(3,aa.get(0)));
for(int l=1;l<kk;l++){
if(l==kk-1){
answer.reserve(ad.substring(aq.get(l-1)+1).trim());
answer.setNum(Integer.parseInt(ad.substring(af[l]+3,aq.get(l-1))));
}
else{
answer.reserve(ad.substring(aq.get(l-1)+1,af[l+1]-1).trim());
answer.setNum((Integer.parseInt(ad.substring(af[l]+3,aq.get(l-1)))));
}
}
}
return answer;
}
public String getdeleteMassage(String ad){
int a=0;
String b;
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)=='-'){
a=i;
}
}
b=ad.substring(a+1);
return b;
}
public void deleteMassage(List<String> a,List<Question> b){
int af=0;
for(int i=0;i<a.size();i++){
for(int j=0;j<b.size();j++){
if(a.get(i).compareTo(b.get(j).getNum())==0){
b.get(j).setIs(false);
af=1;
break;
}
}
if(af==1){
break;
}
}
}
public boolean testIsok(String a){
return a.matches("#T:\\d{1,2}(\\s\\d{0,2}-\\d{0,2})*");
}
public boolean questionIsok(String a){
return a.matches("#N:\\d{1,2}\\s(#Q:.*\\s#A:.*)*");
}
public boolean answerIsok(String a){
return a.matches("#S:\\d{1,2}\\s\\d{8}(\\s#A:\\d{0,2}-\\d{0,2})*");
}
public boolean studentIsok(String a){
return a.matches("#X:\\d{8}\\s\\w{2,5}(-\\d{8}\\s\\w{2,5})*");
}
public boolean deleteIsok(String a){
return a.matches("#D:N-\\d{1,2}");
}
}
public class Main{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
Answer[]answer=new Answer[100];
Student student=new Student();
List<Test> test = new ArrayList<Test>();
Tool tool=new Tool();
List<Question> questions = new ArrayList<Question>();
List<String> delete = new ArrayList<>();
int r=0;
int io=0;
String ad=input.nextLine();
while(ad.compareTo("end")!=0){
if(ad.charAt(1)=='N'){
if(!tool.questionIsok(ad)){
System.out.println("wrong format:"+ad);
}else{
questions.add(tool.getQuestionmassage(ad));
}
}
else if(ad.charAt(1)=='S'){
answer[r]=new Answer();
answer[r]= tool.getAnswermassage(ad);
r++;
/*if(!tool.answerIsok(ad)){
System.out.println("wrong format:"+ad);
}*/
}
else if(ad.charAt(1)=='D'){
delete.add(tool.getdeleteMassage(ad));
if(!tool.deleteIsok(ad)){
System.out.println("wrong format:"+ad);
}
}
else if(ad.charAt(1)=='X'){
student= tool.getStudentmassage(ad);
/*if(!tool.studentIsok(ad)){
System.out.println("wrong format:"+ad);
}*/
}
else if(ad.charAt(1)=='T'){
if(!tool.testIsok(ad)){
System.out.println("wrong format:"+ad);
}else{
test.add(tool.getTestmassage(ad,questions));
}
}
ad=input.nextLine();
}
for(int pq=0;pq<test.size();pq++) {
test.get(pq).getPaper(io);
}
tool.deleteMassage(delete,questions);
for(int i=0;i<r;i++){
answer[i].outPaper(test,student,questions);
}
input.close();
}
}
3.踩坑心得:
就刚开始我把我的输入输出以及对数据的处理全部都存放在main中导致我的类中方法很少,代码的复用性不是很高,然后每一次增加功能,都会导致我要修改的地方特别多,所以我就特地创建了一个工具类,用于做这些对数据的处理,这样就不会导致main中的代码行数特别多,还大大的提升了我的代码的复用性
点击查看代码
public class Main{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
Answer[]answer=new Answer[100];
Test[]test=new Test[100];
List<Question> questions = new ArrayList<Question>();
int r=0;
int io=0;
String ad=input.nextLine();
while(ad.compareTo("end")!=0){
String q="";
String m="";
String w="";
if(ad.charAt(1)=='N'){
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
q=ad.substring(3,j-1);
break;
}
}
int l=0;
for(int j=0;j<ad.length();j++){
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='Q'){
l=j+3;
}
if(ad.charAt(j)=='#'&&ad.charAt(j+1)=='A'){
m=ad.substring(l,j-1);
w=ad.substring(j+3);
}
}
Question ques=new Question(q,m,w);
questions.add(ques);
}
else if(ad.charAt(1)=='S'){
int kk=0;
int[] af=new int[100];
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)=='#'){
af[kk]=i;
kk++;
}
}
if(kk<=1){
answer[r]=new Answer();
answer[r].setBianhao(ad.substring(af[0]+3));
}
else{
answer[r]=new Answer();
answer[r].setBianhao(ad.substring(af[0]+3,af[1]-1));
for(int l=1;l<kk;l++){
if(l==kk-1){
answer[r].reserve(ad.substring(af[l]+3));
}
else{
int iii=af[l]+3;
int op=af[l+1]-1;
answer[r].reserve(ad.substring(iii,op));
}
}
}
r++;
}
else if(ad.charAt(1)=='T'){
int kk=0;
int ll=0;
int[] af=new int[100];
int[] ag=new int[100];
for(int i=0;i<ad.length();i++){
if(ad.charAt(i)==' '){
af[kk]=i;
kk++;
}
if(ad.charAt(i)=='-'){
ag[ll]=i;
ll++;
}
}
test[io]=new Test();
test[io].setBianhao(ad.substring(3,af[0]));
for(int i=0;i<ll;i++){
String aaa=ad.substring(af[i]+1,ag[i]);
String bbb;
if(i==ll-1){
bbb=ad.substring(ag[i]+1);
}
else{
bbb=ad.substring(ag[i]+1,af[i+1]);
}
for(int iw=0;iw<questions.size();iw++){
if(questions.get(iw).getNum().compareTo(aaa)==0){
test[io].setScore(Integer.parseInt(bbb));
test[io].setQuestion(questions.get(iw));
}
}
}
io++;
}
ad=input.nextLine();
}
for(int pq=0;pq<io;pq++){
int total=0;
for(int v=0;v<test[pq].getSize();v++){
total=total+test[pq].getScore(v);
}
if(total!=100){
System.out.println("alert: full score of test paper"+test[pq].getBianhao()+" is not 100 points");
}
}
for(int pp=0;pp<r;pp++){
int yy=0;
for(int pq=0;pq<io;pq++){
if(test[pq].getBianhao().compareTo(answer[pp].getBianhao())==0){
yy=1;
int total=0;
for(int kl=0;kl<answer[pp].getsize();kl++){
if(kl<test[pq].getSize())
System.out.println(test[pq].getQuestion(kl).getNeirong()+"~"+answer[pp].getAnswer(kl)+"~"+test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl)));
}
if(test[pq].getSize()>answer[pp].getsize()){
for(int ij=0;ij<test[pq].getSize()-answer[pp].getsize();ij++){
System.out.println("answer is null");
}
}
if(test[pq].getSize()>answer[pp].getsize()){
for(int kl=0;kl<answer[pp].getsize();kl++){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
for(int kl=0;kl<test[pq].getSize()-answer[pp].getsize();kl++){
if(kl!=test[pq].getSize()-answer[pp].getsize()-1){
System.out.print("0 ");
}
else{
System.out.print("0");
}
}
}
else if(test[pq].getSize()>answer[pp].getsize()){
for(int kl=0;kl<answer[pp].getsize();kl++){
if(kl!=answer[pp].getsize()-1){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
else{
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl));
}
else{
System.out.print("0");
}
}
}
}
else {
for(int kl=0;kl<test[pq].getSize();kl++){
if(kl!=test[pq].getSize()-1){
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl)+" ");
}
else{
System.out.print("0 ");
}
}
else{
if(test[pq].getQuestion(kl).panduan(answer[pp].getAnswer(kl))){
total=total+test[pq].getScore(kl);
System.out.print(test[pq].getScore(kl));
}
else{
System.out.print("0");
}
}
}
}
System.out.println("~"+total);
}
}
if(yy==0){
System.out.println("The test paper number does not exist");
}
}
input.close();
}
}
点击查看代码
public class Main{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
Answer[]answer=new Answer[100];
Student student=new Student();
List<Test> test = new ArrayList<Test>();
Tool tool=new Tool();
List<Question> questions = new ArrayList<Question>();
List<String> delete = new ArrayList<>();
int r=0;
int io=0;
String ad=input.nextLine();
while(ad.compareTo("end")!=0){
if(ad.charAt(1)=='N'){
if(!tool.questionIsok(ad)){
System.out.println("wrong format:"+ad);
}else{
questions.add(tool.getQuestionmassage(ad));
}
}
else if(ad.charAt(1)=='S'){
answer[r]=new Answer();
answer[r]= tool.getAnswermassage(ad);
r++;
/*if(!tool.answerIsok(ad)){
System.out.println("wrong format:"+ad);
}*/
}
else if(ad.charAt(1)=='D'){
delete.add(tool.getdeleteMassage(ad));
if(!tool.deleteIsok(ad)){
System.out.println("wrong format:"+ad);
}
}
else if(ad.charAt(1)=='X'){
student= tool.getStudentmassage(ad);
/*if(!tool.studentIsok(ad)){
System.out.println("wrong format:"+ad);
}*/
}
else if(ad.charAt(1)=='T'){
if(!tool.testIsok(ad)){
System.out.println("wrong format:"+ad);
}else{
test.add(tool.getTestmassage(ad,questions));
}
}
ad=input.nextLine();
}
for(int pq=0;pq<test.size();pq++) {
test.get(pq).getPaper(io);
}
tool.deleteMassage(delete,questions);
for(int i=0;i<r;i++){
answer[i].outPaper(test,student,questions);
}
input.close();
}
}
存储题目时,刚开始是数组,后面虽然也可以用数组,但是,这样我存储的题目数量就被固定了,万一超过了数组的最大范围,就会导致数组越界,使得代码出问题,而且到时候如果还要遍历数组的时候我们得知道里面有多少数据,所以我们还得增加一个变量来计算我们存储了多少数据在里面,所以我就用了集合arraylist,这样就不用担心题目太多而导致代码出问题了,
点击查看代码
Answer[]answer=new Answer[100];
Student student=new Student();
List<Test> test = new ArrayList<Test>();
Tool tool=new Tool();
List<Question> questions = new ArrayList<Question>();
List<String> delete = new ArrayList<>();
int r=0;
int io=0;
点击查看代码
public void outPaper(List<Test> a,Student b,List<Question> c){
int y=0;
for(int i=0;i<a.size();i++){
if(this.bianhao.compareTo(a.get(i).getBianhao())==0){
y=1;
int total=0;
for(int j=0;j<a.get(i).getSize();j++){
if(j<num.size()) {
int ll = 0;
for (int kj = 0; kj < c.size(); kj++) {
if (a.get(i).getQuestion(j).getNum().compareTo(c.get(kj).getNum()) == 0) {
ll = 1;
for (int k = 0; k < a.get(i).getSize(); k++) {
if (this.num.get(j) == k+1) {
if (a.get(i).getQuestion(k).getIs()) {
System.out.println(a.get(i).getQuestion(k).getNeirong() + "~" + this.answer.get(j) + "~" + a.get(i).getQuestion(k).panduan(this.answer.get(j)));
if (a.get(i).getQuestion(k).panduan(this.answer.get(j))) {
total += a.get(i).getScore(k);
} else {
a.get(i).getScore().set(k, 0);
}
}
}
}
if(j==num.size()-1) {
for (int k = 0; k < a.get(i).getSize(); k++) {
if (!a.get(i).getQuestion(k).getIs()) {
a.get(i).getScore().set(k, 0);
System.out.println("the question " + a.get(i).getQuestion(k).getNum() + " invalid~0");
}
}
}
}
}
if (ll == 0) {
System.out.println("non-existent question~0");
}
}
}
for(int sd=0;sd<a.get(i).getSize()-num.size();sd++){
System.out.println("answer is null");
a.get(i).getScore().set(num.size()+sd,0);
}
int lk=0;
for(int p=0;p<b.getSize();p++){
if(this.studentID.compareTo(b.getStudentID(p))==0){
lk=1;
System.out.print(b.getStudentID(p)+" "+b.getName(p)+":");
for(int j=0;j<a.get(i).getSize();j++){
//20201103 Tom: 0 0~0
System.out.print(" "+a.get(i).getScore(j));
if(j==a.get(i).getSize()-1)
System.out.println("~"+total);
}
}
}
if(lk==0) {
System.out.println(this.studentID + " not found");
}
}
}
if(y==0){
System.out.println("The test paper number does not exist");
}
}
}
4改进建议:
我认为我还应该将方法再细分一些,还可以再次提高代码的复用性,虽然我已经有了一个工具类稍微改善了一下,但是在工具类中,代码的圈复杂度还是特别的高,所以还可以通过继续拆分来解决这个问题,将圈复杂度减少,提高代码的可读性,不至于让读者云里雾里,还有就是对于第三次大作业的最后一题了,因为我之前还未找到假如题目被删除,然后再输入一个一样题号的题目,也可以输出正常的解决方法,现在我觉得我可以先将删除的题目boolean变为true,再将新输入的题目输入,改变原题目的所有数据,这样就可以解决这个问题了,还有就是我应该再多学习一点算法,因为按正常的老方法去写,代码量已经比那些优秀的同学的代码长太多了,几乎多了100多行,我还应该去问问他们是如何将代码写的如此简便的。
5,总结:
通过这三次作业,我学会了面对对象语言相交于面对过程语言的区别,面对对象,顾名思义主要是针对对象来写代码的,这个对象有什么属性,有什么行为,而面向过程的,主要在于功能的实现,而没有什么特别的对象要求,我还学会了正则表达式,这个大大的减少了我对if的依赖,减少了我的代码量,而且十分的方便,我还学会了arraylist集合,这样我就可以在不知数量的情况下也能创建出一个类似数组的集合了,不需要再想以前一样,取一个较大的空间数组来存储数据了,不用再浪费空间了。我觉得大作业可以再多增加一点样例,因为即使满足了所有的样例,但却连一半分都拿不到,而且有的测试点的提示是完全一样的,那这种就完全没有意义了,还有的测试点并没有在题目中提出要求,全部都要靠自己一个个找,还有就是我认为老师可以将每次上课讲知识点时所用到代码样例发布在群里,这样有需要的同学就可以根据群里的代码,来复习今天所学的知识。