前三次大作业总结
果然听网课和写代码是两回事,初识Java以为跟c语言差不多(毕竟跟c语言很多语法知识有重合的部分嘛)。结果当我真正去写代码还是有很多的问题,很多基础结构记不清楚 每次都要看下笔记 唉,但经过这三次的大作业的 毒打 锻炼,我果然对java熟悉多了。
第一次大作业
感觉非常简单鸭...跟c语言的很多模式差不多。没有感觉到Java与c语言很大的不同,感觉自己的思维还在c语言的面向过程,没有深刻的体会到面向对象的意义。题量虽然很多,一共有九道题目,但是都比较简单,耗时都不是很长。主要是让我们熟悉java的输入、输出、一些循环方法的使用,还有字符串里一些函数的使用方法(真的太好用了,因为有很多方法,能根据里面的函数想很多简便的算法)。
因为题目简单,所以我的报表... (根本没用到什么类的知识,由于对SourceMonitor的不熟悉后面就不使用了,下一篇blog再来!)。倒是踩了一些坑,主要还是关于测试点方面的问题。
第二次大作业
比第一次的大作业要难一点,更加深层次地研究了输入、输出方面的问题,还有循环、字符串的合理运用。虽然是第二次作业了,但自己面向对象的框架结构还是很差,没有什么类的概念。题量不算很大-三道题,还是耗费了一些时间。深刻体会到,写代码前的思考的重要性,直接去敲代码是很不可取的,应该先弄明白自己的需求,再去梳理框架。虽然前期耗费时间较长,但是后期敲代码时目的性会更强,不会再耗费时间去看题目。由于第一题和第三题都比较简单,主要还是输入输出方面的检测问题还有字符串的函数运用,所以这里重点分析第二题。

附上我的源码:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner (System.in); 7 String s=in.nextLine(); 8 int num=1; 9 int begin=0; 10 int end=0; 11 int number=0; 12 if(s.length()<11||s.indexOf('0')==-1) { 13 System.out.println("null data"); 14 return ; 15 } 16 while(s.length()>=11) { 17 begin=s.indexOf('0'); 18 end=begin+8; 19 String s1=s.substring(begin+1,end+2); 20 for(int i=0;i<s1.length();i++) { 21 if(s1.charAt(i)=='1') 22 number++; 23 } 24 if(s.charAt(end+2)=='0') { 25 System.out.println(num+":validate error"); 26 num++; 27 } 28 if(s.charAt(end+2)=='1') { 29 if(number%2==0) 30 System.out.println(num+":parity check error"); 31 32 else 33 System.out.println(num+":"+s.substring(begin+1,end+1)); 34 num++; 35 } 36 37 s=s.substring(end+3); 38 number=0; 39 } 40 41 42 } 43 }
设计与分析: 感觉自己的设计真的很巧妙啊!可能因为我有时候懒得敲很多代码 我时常会想一个简单一点的方法去实现一些需求,我把每次八位数据单独提取出来,并记录相应的开头和结尾,去单独检查与判断,然后再自己去修改字符串的长度让它从前一个八位数据的结尾开始,形成一个循环。这个题目让我再次感叹字符串中一些函数的便捷,java太好用了。
采坑心得:写完代码提交后,总有两个测试点过不去,我突然对自己产生了怀疑,去询问了一下同学,看了一下别人的代码,感觉别人的方法也不错 我还是感觉我的最便捷,没看出自己有什么很不一样的大问题,后来才发现自己在循环条件那里少了一个= (本应该是>= 写成了 > )。中间也有几次搞不清substring 的开始位置,但实践出真谛,用debug测出来了。
改进建议:感觉自己的代码习惯很差,经常依赖与测试点来寻找问题与不足,不愿意多去输入几次数据去测试,并且没有什么写注释的习惯,可读性很差,以后还是要养成写注释的习惯。依旧是没用到什么类...
第三次大作业
感觉难度跟前两次的相比完全是一次大跃进啊 !感觉自己写了个课设了 而且由于前两次的作业,我以为这次不会很难,并且看了下只有三道题。结果等没剩几天,我再来写的时候,完全有一种地狱的感觉,可能我一开始还不太熟练的,直接因为这次作业,完全意识到了类的一些使用方法。通过这次作业,我感觉数学真的是太重要了,很多时候不是代码的问题,是数学的问题,因为忘记了很多公式,直接会走很多弯路。以前不理解为什么都说代码很重要的一部分是数学,现在完全理解了!以后我一定早早地开始写pta 因为最后没时间了,很多功能没实现的我留下痛苦的眼泪。写注释真的太重要了,写多了函数,到后面完全记不清自己一些功能在哪个函数里了,找起来特别费劲。这次作业用到了一个新的知识点“正则表达式”的使用。之前一直追求尽量完美的我...这次留下痛苦的眼泪...
接下来...就是看看这三道题目吧...

附上我的源码:
import java.util.Scanner; public class Main { public static class point{ double x; double y; } public static void main(String[] args) { point n1 = new point(); point n2 = new point(); check_input(n1,n2); } /* * 输入:两个点 * 处理:检查输入格式是否正确,并且得到两点坐标 * 输出:错误格式结果 * */ public static void check_input(point n1,point n2){ double[] array =new double[4]; Scanner in = new Scanner(System.in); int end=0; int begin=0; String s2=new String(); String s = in.nextLine(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==4) { System.out.print("wrong number of points"); return ; } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); return ; } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i==2) { System.out.print("Wrong Format"); return ; } n1.x=array[0]; n1.y=array[1]; n2.x=array[2]; n2.y=array[3]; jisuan(n1,n2); } /* * 输入:两个点 * 处理:计算得到两点的距离 * 输出:两点距离 * */ public static void jisuan(point n1,point n2) { double dis; double m; m=Math.pow(n1.x-n2.x,2.0)+Math.pow(n1.y-n2.y,2.0); dis=Math.pow(m, 0.5); System.out.print(dis); } }
设计与分析:

主要就是两个功能 :检查判断与计算。利用正则表达式去检查判断输入格式是否正确,这里我运用了循环,在每次读出一个坐标去检查是否符合格式。再将合格字符串转化为浮点数类型去记录在建立的point中。最后便是简单的计算过程,再输出结果。感觉整体来说流程还是非常简单的。主要是格式判断的正则表达式部分需要花费一点时间去学习了解 写得晚所以在上课时得到了老师的提醒:用正则表达式算不算写得晚的好处 哭泣。
采坑心得: 感觉题目给的一些东西不是很齐全啊,很多错误格式我是真的找不到啊!感觉自己想不到太多的错误格式,有的想到的错误格式也不是测试点,真的会心累累啊。还是发现自己的习惯也不是很好,很多时候,我还是靠着测试点来发现自己的一些缺点与漏洞。还是要在测试环节多花费一点时间。
改进建议:这次的作业里我添加了注释,果然可读性好多了。但在这里对类的运用还不是很熟悉,并没有体现这一方面的运用。还是不要单纯的去依靠平台的测试点吧,尽量在开始前就完整的设计几个测试点,并根据测试点完善自己的代码。

附上我的源码:
import java.util.IllegalFormatCodePointException; import java.util.Scanner; public class Main{ public static class point{ double x; double y; } public static class line{ point a=new point(); point b=new point(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s=new String(); s=in.nextLine(); String s1=s.substring(0, 2); String regex1 = "[1-5]+:"; boolean flag = s1.matches(regex1);//判断功能 if(!flag) { System.out.print("Wrong Format"); return ; } s=s.substring(2); int num = Integer.parseInt(s1.substring(0,1)); check_input(s,num); } /* * 输入:字符串和选择 * 处理:根据选择去进入相应的检查函数 * 输出: * */ public static void check_input(String s,int num) { switch(num) { case 1:check1(s);break; case 2:check2(s,num);break; case 3:check2(s,num);break; case 4:check3(s,num);break; case 5:check3(s,num);break; } } /* * 输入:字符串 * 处理:检查输入格式是否正确并且得到点坐标 * 输出:格式错误 * */ public static void check1(String s) { double[] array =new double[4]; line n1=new line(); String sm=s; int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==4) { System.out.print("wrong number of points"); return ; } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); return ; } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<3) { System.out.print("wrong number of points"); return; } n1.a.x=array[0]; n1.a.y=array[1]; n1.b.x=array[2]; n1.b.y=array[3]; if(Math.abs(n1.a.x-n1.b.x)<0.00001&&Math.abs(n1.a.y-n1.b.y)<0.00001) { System.out.print("points coincide"); return ; } prime_xl(n1); } /* * 输入:一条直线上的两点 * 处理:计算斜率并判断 * 输出:斜率 * */ public static void prime_xl (line n1) { if(Math.abs(n1.a.x-n1.b.x)<0.0001) { System.out.print("Slope does not exist"); return ; } double xl; xl=(n1.a.y-n1.b.y)/(n1.a.x-n1.b.x); System.out.print(xl); } /* * 输入:字符串和选择 * 处理:检查输入格式是否正确并且得到点坐标并进入相应的选择 * 输出:格式错误 * */ public static void check2(String s,int num) { double[] array =new double[6]; line n1=new line(); point m1=new point(); int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==6) { System.out.print("wrong number of points"); return ; } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); return ; } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<5) { System.out.print("wrong number of points"); return; } n1.a.x=array[0]; n1.a.y=array[1]; n1.b.x=array[2]; n1.b.y=array[3]; m1.x=array[4]; m1.y=array[5]; if(Math.abs(n1.a.x-n1.b.x)<0.001&&Math.abs(n1.a.y-n1.b.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n1.a.x-m1.x)<0.001&&Math.abs(n1.a.y-m1.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(m1.x-n1.b.x)<0.001&&Math.abs(m1.y-n1.b.y)<0.001) { System.out.print("points coincide"); return ; } if(num==2) length(n1,m1); if(num==3) prime_gx(n1,m1); } /* * 输入:一条直线一个点 * 处理:计算点到直线的垂直距离 * 输出:距离 * */ public static void length(line n1,point m1) { double k; double l=0; if(Math.abs(n1.b.x-m1.x)<0.0001) { l=Math.abs(m1.x-n1.a.x); } else{ k = (n1.b.y-m1.y)/(n1.b.x-m1.x); if(Math.abs(k)<0.0001) { l=Math.abs(m1.y-n1.a.y); } else { l=Math.abs(n1.a.y-k*n1.a.x+k*n1.b.x-n1.b.y)/Math.sqrt(1+Math.pow(k,2)); } } System.out.print(l); } /* * 输入:一条直线上的两点和一个点 * 处理:判断是否共线 * 输出:true 或者 false * */ public static void prime_gx(line n1,point m1) { double x=(m1.y-n1.a.y)*(n1.b.x-n1.a.x)-(n1.b.y-n1.a.y)*(m1.x-n1.a.x); if( Math.abs(x) <0.00001 ) { System.out.print("true"); } else { System.out.print("false"); } } /* * 输入:字符串和选择 * 处理:检查输入格式是否正确并且得到点坐标并进入相应的选择 * 输出:格式错误 * */ public static void check3(String s,int num) { double[] array =new double[8]; line n1=new line(); line n2=new line(); int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==8) { System.out.print("wrong number of points"); return ; } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); return ; } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<7) { System.out.print("wrong number of points"); return; } n1.a.x=array[0]; n1.a.y=array[1]; n1.b.x=array[2]; n1.b.y=array[3]; n2.a.x=array[4]; n2.a.y=array[5]; n2.b.x=array[6]; n2.b.y=array[7]; if(Math.abs(n1.a.x-n1.b.x)<0.001&&Math.abs(n1.a.y-n1.b.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n2.a.x-n2.b.x)<0.001&&Math.abs(n2.a.y-n2.b.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n1.a.x-n2.a.x)<0.001&&Math.abs(n1.a.y-n2.a.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n1.b.x-n2.b.x)<0.001&&Math.abs(n1.b.y-n2.b.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n1.a.x-n2.b.x)<0.001&&Math.abs(n1.a.y-n2.b.y)<0.001) { System.out.print("points coincide"); return ; } if(Math.abs(n1.b.x-n2.a.x)<0.001&&Math.abs(n1.b.y-n2.a.y)<0.001) { System.out.print("points coincide"); return ; } if(num==5) distant(n1,n2); if(num==4) System.out.print(prime_px(n1,n2)); } /* * 输入:两条直线 * 处理:计算判断是否平行 * 输出:true 或者 false * */ public static boolean prime_px(line n1,line n2) { double p1=Math.abs(n1.a.x-n1.b.x); double p2=Math.abs(n2.a.x-n2.b.x); if(Math.abs(p1-p2)<0.00001) { return true; } else { double p3=(n1.a.y-n1.b.y)/(n1.a.x-n1.b.x); double p4=(n2.a.y-n2.b.y)/(n2.a.x-n2.b.x); if(Math.abs(p3-p4)<0.00001) { return true; } else { return false; } } } /* * 输入:两条直线 * 处理:计算两个点所构成的直线与后两点构成的直线的交点坐标,并判断交叉点是否在两条线段之内 * 输出:交点坐标和判断结果(true/false) * */ public static void distant(line n1,line n2) { if(prime_px(n1,n2)) { System.out.print("is parallel lines,have no intersection point"); } else { double x1=n1.a.x; double x2=n1.b.x; double x3=n2.a.x; double x4=n2.b.x; double y1=n1.a.y; double y2=n1.b.y; double y3=n2.a.y; double y4=n2.b.y; point con=new point(); con.x=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3); con.y=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3); System.out.print(con.x+","+con.y+" "); boolean p1= (Math.abs((con.x - n1.a.x)*(n1.a.y - n1.b.y) - (n1.a.x - n1.b.x)*(con.y - n1.a.y))<0.00001); boolean p2=(Math.abs((con.x - n2.a.x)*(n2.a.y - n2.b.y) - (n2.a.x - n2.b.x)*(con.y - n2.a.y))<0.00001); boolean p3; boolean p4; if(x1<x2) { p3= con.x>x1 &&con.x<x2; } else { p3= con.x<x1&&con.x>x2; } if(x3<x4) { p4= con.x>x3 &&con.x<x4; } else { p4= con.x<x3&&con.x>x4; } if((p1&&p3)||(p2&&p4)) { System.out.print("true"); } else { System.out.print("false"); } } } }
设计与分析:

由于老师上课的提醒,我对于类的运用更加熟悉了。线上的点,我引用了Point 类去构成线,增加类间的交互性,代码确实更加清晰并且方便操作。 一开始,我仍是对输入字符串运用正则表达式去检测格式,得到正确坐标,再进行相应的操作
采坑心得:很多关于数学方面的问题还是要去百度,因为我写的晚,没有很多时间让我去思考方法和学习方法,这导致自己在这道题后学到的东西大打折扣!并且中途有个计算的测试点一直过不去,看了半天才发现是自己看错了题目,导致浪费了很多时间!气死我了!以后一定要带着自己的眼睛 对,不是眼镜 去读题!其中还是有关于自己格式的问题的测试点过不去,由于有上一题的多次测试,还是无法通过,我就果断 放弃了 下一题了。

改进建议:自己虽然加了注释,但还是没怎么运用到类,没有将一些操作与判断分开,简洁性较差,并且在检查判断方面有很多重复的代码,并没有找到好的方法将这些合并起来,增加代码的复用性,让代码更简洁。

附上我的源码:
import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static class point{ double x; double y; } public static class triangle{ point a=new point(); point b=new point(); point c=new point(); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s=new String(); s=in.nextLine(); String s1=s.substring(0, 2); String regex1 = "[1-5]+:"; boolean flag = s1.matches(regex1);//判断功能 if(!flag) { System.out.print("Wrong Format"); System.exit(0); } s=s.substring(2); int num = Integer.parseInt(s1.substring(0,1)); check.check_input(s,num); } /* * 输入:字符串和选择 * 处理:根据选择去进入相应的检查函数 * 输出: * */ public static class check{ public static void check_input(String s,int num) { switch(num) { case 1:check1(s,num);break; case 2:check1(s,num);break; case 3:check1(s,num);break; case 4:check2(s);break; case 5:check3(s);break; } } /* * 输入:字符串和选择 * 处理:检查输入格式是否正确并且得到点坐标并进入相应的选择 * 输出:格式错误 * */ public static void check1(String s,int num) { double[] array =new double[6]; triangle n1=new triangle(); int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==6) { System.out.print("wrong number of points"); System.exit(0); } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); System.exit(0); } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<5) { System.out.print("wrong number of points"); System.exit(0); } n1.a.x=array[0]; n1.a.y=array[1]; n1.b.x=array[2]; n1.b.y=array[3]; n1.c.x=array[4]; n1.c.y=array[5]; if(Math.abs(n1.a.x-n1.b.x)<0.001&&Math.abs(n1.a.y-n1.b.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(n1.a.x-n1.c.x)<0.001&&Math.abs(n1.a.y-n1.c.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(n1.c.x-n1.b.x)<0.001&&Math.abs(n1.c.y-n1.b.y)<0.001) { System.out.print("points coincide"); System.exit(0); } switch(num) { case 1:handle.prime_lx(n1);break; case 2:handle.output_sj(n1);break; case 3:handle.prime_jd(n1);break; } } /* * 输入:字符串 * 处理:检查输入格式是否正确并且得到点坐标 * 输出:格式错误 * */ public static void check3(String s) { double[] array =new double[8]; triangle n1=new triangle(); point w=new point(); int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==8) { System.out.print("wrong number of points"); System.exit(0); } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); System.exit(0); } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<7) { System.out.print("wrong number of points"); System.exit(0); } w.x=array[0]; w.y=array[1]; n1.a.x=array[2]; n1.a.y=array[3]; n1.b.x=array[4]; n1.b.y=array[5]; n1.c.x=array[6]; n1.c.y=array[7]; if(Math.abs(w.x-n1.a.x)<0.001&&Math.abs(w.y-n1.a.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(w.x-n1.b.x)<0.001&&Math.abs(w.y-n1.b.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(w.x-n1.c.x)<0.001&&Math.abs(w.y-n1.c.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(n1.a.x-n1.b.x)<0.001&&Math.abs(n1.a.y-n1.b.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(n1.a.x-n1.c.x)<0.001&&Math.abs(n1.a.y-n1.c.y)<0.001) { System.out.print("points coincide"); System.exit(0); } if(Math.abs(n1.b.x-n1.c.x)<0.001&&Math.abs(n1.b.y-n1.c.y)<0.001) { System.out.print("points coincide"); System.exit(0); } handle.prime_in(n1,w); } /* * 输入:字符串 * 处理:检查输入格式是否正确并且得到点坐标 * 输出:格式错误 * */ public static void check2(String s) { double[] array =new double[10]; triangle n1=new triangle(); point w=new point(); point d=new point(); int end=0; int begin=0; String s2=new String(); int i=0; int m=0; String s3=new String(); String s4=new String(); while(s.length()>0) { if(i==10) { System.out.print("wrong number of points"); System.exit(0); } end=s.indexOf(' '); if(end==-1) { s2=s.substring(begin); } else { s2=s.substring(begin, end); } String regex = "[+-]?([0-9]+[.])?[0-9]+,[+-]?([0-9]+[.])?[0-9]+"; boolean flag = s2.matches(regex);//判断功能 if(!flag) { System.out.print("Wrong Format"); System.exit(0); } m=s2.indexOf(','); s3=s2.substring(begin, m); s4=s2.substring(m+1); array[i]=Double.parseDouble(s3); array[i+1]=Double.parseDouble(s4); i=i+2; if(end!=-1) { s=s.substring(end+1); } else { s=s.substring(s.length()); } } if(i<9) { System.out.print("wrong number of points"); System.exit(0); } w.x=array[0]; w.y=array[1]; d.x=array[2]; d.y=array[3]; n1.a.x=array[4]; n1.a.y=array[5]; n1.b.x=array[6]; n1.b.y=array[7]; n1.c.x=array[8]; n1.c.y=array[9]; if(Math.abs(w.x-d.x)<0.001&&Math.abs(w.y-d.y)<0.001) { System.out.print("points coincide"); System.exit(0); } handle.prime_xj(n1, w, d); } } public static class handle { /* * 输入:三个点 * 处理:判断是否是等腰三角形、等边三角形 * 输出:结果输出true/false * */ public static void prime_lx(triangle n1) { prime(n1); double a=jisuandis(n1.a,n1.b); double b=jisuandis(n1.a,n1.c); double c=jisuandis(n1.b,n1.c); if(Math.abs(a-b)<0.0001||Math.abs(c-b)<0.0001||Math.abs(a-c)<0.0001) { System.out.print("true "); } else { System.out.print("false "); } if(Math.abs(a-b)<0.0001&&Math.abs(c-b)<0.0001) { System.out.print("true"); } else { System.out.print("false"); } } /* * 输入:三个点 * 处理:判断能否形成三角形 * 输出:错误则输出 结束程序 * */ public static void prime(triangle n1) { double a=jisuandis(n1.a,n1.b); double b=jisuandis(n1.a,n1.c); double c=jisuandis(n1.b,n1.c); double t=0; if(a>=b) { t=a;a=b;b=t; } if(b>=c) { t=b;b=c;c=t; } if(a>=b) { t=a;a=b;b=t; } if( (a+b)<=c ) { System.out.print("data error"); System.exit(0); } double x=(n1.c.y-n1.a.y)*(n1.b.x-n1.a.x)-(n1.b.y-n1.a.y)*(n1.c.x-n1.a.x); if( Math.abs(x) <0.00001 ) { System.out.print("data error"); System.exit(0); } } /* * 输入:两个点 * 处理:计算两点间的距离 * 输出:距离 * */ public static double jisuandis(point n1,point n2) { double dis; double m; m=Math.pow(n1.x-n2.x,2.0)+Math.pow(n1.y-n2.y,2.0); dis=Math.pow(m, 0.5); return dis; } /* * 输入:三角形三个点 * 处理:计算周长、面积、重心坐标 * 输出:周长、面积、重心坐标 * */ public static void output_sj(triangle n1) { double a=jisuandis(n1.a,n1.b); double b=jisuandis(n1.a,n1.c); double c=jisuandis(n1.b,n1.c); double zc =a+b+c; double p = (a + b + c) / 2; double s=Math.sqrt(p * (p - a) * (p - b) * (p - c)); double x=(n1.a.x+n1.b.x+n1.c.x)/3; double y=(n1.a.y+n1.b.y+n1.c.y)/3; DecimalFormat df =new DecimalFormat("0.0#####"); System.out.print(df.format(zc)+" "+df.format(s)+" "+df.format(x)+","+df.format(y)); } /* * 输入:三角形三个点 * 处理:计算判定是钝角、直角还是锐角三角形 * 输出:三个判断结果(true/false) * */ public static void prime_jd(triangle n1) { prime(n1); double a=jisuandis(n1.a,n1.b); double b=jisuandis(n1.a,n1.c); double c=jisuandis(n1.b,n1.c); double t=0; if(a>=b) { t=a;a=b;b=t; } if(b>=c) { t=b;b=c;c=t; } if(a>=b) { t=a;a=b;b=t; } if(c*c > (a*a+b*b)) { System.out.print("true "); } else { System.out.print("false "); } if(Math.abs(c*c-(a*a+b*b))<0.0001) { System.out.print("true "); } else { System.out.print("false "); } if(c*c < (a*a+b*b)) { System.out.print("true"); } else { System.out.print("false"); } } /* * 输入:四个点坐标 * 处理:判断第一个点是否在后三个点所构成的三角形的内部(射线法) * 输出:输出in the triangle/outof triangle * */ public static void prime_in(triangle n1,point w) { prime(n1); point t=new point(); if(n1.a.x>=n1.b.x) { t=n1.a ; n1.a=n1.b ; n1.b=t; } if(n1.b.x>=n1.c.x) { t=n1.b;n1.b=n1.c;n1.c=t; } if(n1.a.x>=n1.b.x) { t=n1.a;n1.a=n1.b;n1.b=t; } double a=jisuan_xl(n1.a,n1.b); double b=jisuan_xl(n1.a,n1.c); double c=jisuan_xl(n1.b,n1.c); double a1=a*w.x-n1.a.y; double b1=b*w.x-n1.b.y; double c1=c*w.x-n1.c.y; if(pd(a1,w.y)&&(w.x<=n1.b.x)&&(w.x>=n1.a.x)) { System.out.print("on the triangle"); System.exit(0); } if(pd(b1,w.y)&&(w.x<=n1.c.x)&&(w.x>=n1.a.x)) { System.out.print("on the triangle"); System.exit(0); } if(pd(c1,w.y)&&(w.x<=n1.c.x)&&(w.x>=n1.b.x)) { System.out.print("on the triangle"); System.exit(0); } boolean kw = PointInFences(w,n1); if(kw) { System.out.print("in the triangle"); } else { System.out.print("outof triangle"); } } private static boolean PointInFences(point pnt1, triangle n1) { point[] fencePnts =new point[3]; fencePnts[0]=n1.a; fencePnts[1]=n1.b; fencePnts[2]=n1.c; int j=0, cnt = 0; for (int i = 0; i < fencePnts.length; i++) { j = (i == fencePnts.length - 1) ? 0 : j + 1; if ((fencePnts[i].y!=fencePnts[j].y)&&(((pnt1.y >= fencePnts[i].y) && (pnt1.y < fencePnts[j].y)) || ((pnt1.y >= fencePnts[j].y) && (pnt1.y < fencePnts[i].y))) && (pnt1.x < (fencePnts[j].x - fencePnts[i].x) * (pnt1.y - fencePnts[i].y) / (fencePnts[j].y - fencePnts[i].y) + fencePnts[i].x)) cnt++; } return (cnt%2>0)?true:false; } public static void prime_xj(triangle n1,point w,point d) { prime(n1); point t=new point(); if(n1.a.x>=n1.b.x) { t=n1.a ; n1.a=n1.b ; n1.b=t; } if(n1.b.x>=n1.c.x) { t=n1.b;n1.b=n1.c;n1.c=t; } if(n1.a.x>=n1.b.x) { t=n1.a;n1.a=n1.b;n1.b=t; } if(Math.abs(w.x-d.x)<0.0001) { if(pd(w.x,n1.a.x)||pd(w.x,n1.c.x)) { System.out.print("1"); } else if(w.x<n1.a.x||w.x>n1.c.x) { System.out.print("0"); } else { } } else { double f=jisuan_xl(w,d); double a=jisuan_xl(n1.a,n1.b); double b=jisuan_xl(n1.a,n1.c); double c=jisuan_xl(n1.b,n1.c); double f1=f*w.x-w.y; double a1=a*n1.a.x-n1.a.y; double b1=b*n1.b.x-n1.b.y; double c1=c*n1.c.x-n1.c.y; if((pd(f,a)&&pd(f1,a1))||(pd(f,b)&&pd(f1,b1))||(pd(f,c)&&pd(f1,c1))) { System.out.print("The point is on the edge of the triangle"); System.exit(0); } double y1=f*n1.a.x-f*w.x+w.y; double y2=f*n1.b.x-f*w.x+w.y; double y3=f*n1.c.x-f*w.x+w.y; if(pd(y1,n1.a.y)||pd(y2,n1.b.y)||pd(y3,n1.c.y)) { System.out.print("1"); } } } public static boolean pd(double n,double m) { if(Math.abs(n-m)<0.001) return true; else return false; } public static double jisuan_xl(point n1,point n2) { double xl; xl=(n1.y-n2.y)/(n1.x-n2.x); return xl; } } }
设计与分析:

这次是真正用到了类。过程依旧是先对输入的字符串进行检查格式,再根据第一个字符进入相应的操作。每个操作函数我都有相应的注释,增加可读性。我将操作与检查判断放到了另外的两个类中,让代码更加清晰易懂。
采坑心得: 还是由于自己开始作业的时间较晚,没有很多的时间去学习一些数学方法或者自己设计一些模型去解决需求。所以第四点和第五点需求完成度很差,大家可以忽略...并且由于题目的较难并且没时间的原因,我也很难去自己设计一些测试点去判断自己代码的正确性,下一次一定一定要多留一些时间去写作业!

改进建议:虽然自己运用到了类,但是在check类中还是有很多重复性的操作,并没有找到好的方法去统一的完成这个检查与判断问题。并且很多操作函数里也有一些重复性的操作,完全可以再写个函数去统一的完成这些操作,简化代码。关于数学方面还需要多多加强。在敲代码开始前还是多花点时间思考设计一些测试点吧,再根据测试点完善自己的代码。
总结
这三次作业我进一步熟悉了输入输出,还有字符串中一些函数的运用。关于类的运用我还是需要自己进一步的学习以及实践。
感觉通过这三次大作业,才真正的体会到Java的魅力,算是个初初初级入门选手了。在完成作业时,我也体会到了自己很多知识储备的不完善,单上网课(线上与“线下”)还是不够的,还是需要回归课本,将那本厚厚的书认真完全的阅读了解一遍 (应该不止一遍)。自己自学的能力还是要加强,不能单纯依靠老师,毕竟课时有限,还是要自己去拓展见识。还有,博客自带的编辑器真的很不好用啊! 一开始把我很多内容吞了是什么意思啊!气死我了!

浙公网安备 33010602011771号