PTA-Blog-
又是一次次的大作业,三角形四边形五边形,一次次的进阶升级,虽然只是多了一条边,但是这个难度不是多了单单一点点,前面几点还好,判断四边形啊,判断形状啊,但是后面那几点,线于形的交点数量,刚开始写的时候属实是摸不着头脑,虽然后来也是这样【小声bb】,随着写的代码所需越来越多,适当运用方法,能使Main函数显得更整洁,这种选项的题目,在每个选项前加个序号,也能让自己分清所写的代码是什么
m=shu(x); //判断点的数量
//判断点的数量,返回m的值:2为两个点以此类推,0为输入点不符合要求 public static int shu(String x) { int i,m=0; for(i=0;i<x.length();i++){ if(x.charAt(i) == ','){ m++; } } return m; }
`//1.判断四边形
if(x.charAt(0)=='1') {
String[] str=x.split(" ");
String[] str1=str[0].split(",");
String[] str2=str[1].split(",");
String[] str3=str[2].split(",");
String[] str4=str[3].split(",");
String[] str_=str1[0].split("😊;
double x1=Double.parseDouble(str_[1]);
double y1=Double.parseDouble(str1[1]);
double x2=Double.parseDouble(str2[0]);
double y2=Double.parseDouble(str2[1]);
double x3=Double.parseDouble(str3[0]);
double y3=Double.parseDouble(str3[1]);
double x4=Double.parseDouble(str4[0]);
double y4=Double.parseDouble(str4[1]);
if(m!=4)
System.out.print("wrong number of points");
else if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x1==x4)&&(y1==y4))||
((x2==x3)&&(y2==y3))||((x2==x4)&&(y2==y4))||((x3==x4)&&(y3==y4)))
System.out.print("points coincide");
else{
int m1=0;
int n1=0;
m1=four(x);
n1=pxfour(x);
if(m1==0) {
System.out.print("false ");
}
else System.out.print("true ");
if(n1==0){
System.out.print("false");
}
else System.out.print("true");
}
}
//2.菱形矩形正方形
if(x.charAt(0)=='2') {
String[] str=x.split(" ");
String[] str1=str[0].split(",");
String[] str2=str[1].split(",");
String[] str3=str[2].split(",");
String[] str4=str[3].split(",");
String[] str_=str1[0].split(":");
double x1=Double.parseDouble(str_[1]);
double y1=Double.parseDouble(str1[1]);
double x2=Double.parseDouble(str2[0]);
double y2=Double.parseDouble(str2[1]);
double x3=Double.parseDouble(str3[0]);
double y3=Double.parseDouble(str3[1]);
double x4=Double.parseDouble(str4[0]);
double y4=Double.parseDouble(str4[1]);
if(m!=4)
System.out.print("wrong number of points");
else if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x1==x4)&&(y1==y4))||
((x2==x3)&&(y2==y3))||((x2==x4)&&(y2==y4))||((x3==x4)&&(y3==y4)))
System.out.print("points coincide");
int m2=0,n2=0;
m2=four(x);
n2=pxfour(x);
if(m2==0) {
System.out.println("not a quadrilateral");
}
else {
if(n2==1) {
int mm2=0,nn2=0;
mm2=same(x);
nn2=chuizhi(x);
//菱形
if(mm2==1&&nn2==0)
{
System.out.print("true ");
}
else System.out.print("false ");
//正方形
if(mm2==1&&nn2==1)
{
System.out.print("true ");
}
else System.out.print("false ");
//矩形
if(mm2==0&&nn2==1)
{
System.out.print("true");
}
else System.out.print("false");
}
}
}`
上面这种自我感觉就很行。
这次也有选项嘛,同样可以运用到像charAt()函数,可以用来判断输入的数值,具体在某一位,例如1234的charAt(1)为‘2’;如需使用数学方法的话,就要在方法面前加Math以正常运行,substring的截取字符串,indexof的寻找目标第一次出现的下标,split的字符串切割,强制转换类型,matches的匹配方式,以及正则表达式,这次为了方便,不再惯用上次的正则表达式例如
if(x.matches("^[1-5]?:([+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)? ?)+$")!=true)
然后第四次作业的银行业务,就主要是要写出bank的属性
BankBusiness(String name,int password){ this.name=name; this.password=password; balance=0; }
写出这个就会好写很多。

在这个四边形题目中,最主要就是看你的逻辑思维,如果连数学题目都不会做的话,就甭提写代码了,我就是这种情况,所以才要好好地学数学
//期中考试
说起这次的期中考试,就来气,本来稳稳的及格线以上的,在一连串的代码下,
//输出坐标信息 public void display() { System.out.println("("+String.format("%.2f", x)+"),("+String.format("%.2f", y)+")"); }
这个输出的是(x),(y),这么简单的(x,y),我是真的服,在这个地方也花了好多时间,大意了这波没有在测试区看看结果,就是单纯的提交本题作答,一错再错。。无大语破大防。
整体像这样
`import java.util.*;
import java.lang.String;
public class Main {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
double x,y;
x=input.nextDouble();
y=input.nextDouble();
Point p1=new Point(x,y);
x=input.nextDouble();
y=input.nextDouble();
Point p2=new Point(x,y);
String str=new String();
str=input.next();
Line line1=new Line(p1,p2,str);
if(p1.getX()<0||p2.getX()<0||p1.getX()>200||p2.getX()>200||
p1.getY()<0||p2.getY()<0||p1.getY()>200||p2.getY()>200) {
System.out.println("Wrong Format");
}
else line1.display();
}
}
//点类
class Point{
private double x,y;
Point(){
x=0;
y=0;
}
Point(double x,double y){
this.x=x;
this.y=y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x=x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y=y;
}
//输出坐标信息
public void display() {
System.out.println("("+String.format("%.2f", x)+"),("+String.format("%.2f", y)+")");
}
}
//线类
class Line{
private Point p10;
private Point p20;
private String color;
Line(){
this.p10=new Point();
this.p20=new Point();
this.color=new String();
}
Line(Point p1,Point p2,String color){
this.p10=p1;
this.p20=p2;
this.color=color;
}
Point getPoint1() {
return p10;
}
public void setPoint1(Point p1) {
this.p10=p1;
}
public Point getPoint2() {
return p20;
}
public void setPoint2(Point p2) {
this.p20=p2;
}
public String getColor() {
return color;
}
public void setColor(String str) {
this.color=str;
}
public double getDistance() {
return Math.sqrt(Math.pow(p10.getX()-p20.getX(),2)+Math.pow(p10.getY()-p20.getY(),2));
}
public void display() {
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
p10.display();
System.out.println("The line's end point's Coordinate is:");
p20.display();
System.out.println("The line's length is:"+String.format("%.2f",getDistance()));
}
}第五次作业,判断凹凸五边形,不知为什么计算错误,自己想着是先计算每一条线另外的三点是否在直线的同一侧,如果都在同一侧且五条直线都是如此就为凸五边形,然后我就写了以下代码ouble a,b,c,s=0,n=0,z=0;
int l=0,r=0;
a=y1-y2;
b=x1-x2;
c=(x1-x2)y2-x1(y1-y2);
s=a*x3+b*y3+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
s=a*x4+b*y4+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
s=a*x5+b*y5+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
if(l==3||r==3)
z++;`
这里的a,b,c是计算出两点所在的直线,s是将另外几点带入坐标的数值,大于零则在右边,小于零则在左边,l计算右点的数量,r计算左点的数量,如果在线上的话,n就加一,n一旦不为初始值,就说明这五个点不能组成一个五边形,每一条直线计算完毕,如果满足另外三点都在同一侧的话,z加一,在五条直线都计算完后,若z为5,则为凸五边形,z不为5,则为凹五边形,不知道这段代码有什么问题,不让过,可能有些问题没有发现,就着我的思路来,是这样的没错,可就是出了问题。
整体这样
`import java.util.;
import java.util.regex.;
import java.lang.String;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String x=in.nextLine();
int m=0,i=0;
for(i=0;i<x.length();i++){
if(x.charAt(i) == ','){
m++;
}
}
if(x.matches("^[1-3]?:([+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)? ?)+$")!=true)
System.out.println("Wrong Format");
else {
//1.判断五边形
if(x.charAt(0)=='1') {
String[] str=x.split(" ");
String[] str1=str[0].split(",");
String[] str2=str[1].split(",");
String[] str3=str[2].split(",");
String[] str4=str[3].split(",");
String[] str5=str[4].split(",");
String[] str_=str1[0].split(":");
double x1=Double.parseDouble(str_[1]);
double y1=Double.parseDouble(str1[1]);
double x2=Double.parseDouble(str2[0]);
double y2=Double.parseDouble(str2[1]);
double x3=Double.parseDouble(str3[0]);
double y3=Double.parseDouble(str3[1]);
double x4=Double.parseDouble(str4[0]);
double y4=Double.parseDouble(str4[1]);
double x5=Double.parseDouble(str5[0]);
double y5=Double.parseDouble(str5[1]);
if(m!=5)
System.out.print("wrong number of points");
else if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)||
(y3-y2)/(x3-x2)==(y4-y2)/(x4-x2)||
(y4-y3)/(x4-x3)==(y5-y3)/(x5-x3)||
(y5-y4)/(x5-x4)==(y1-y4)/(x1-x4))
System.out.print("false");
else if(m==5) {
System.out.print("true");
}
}
//2.凹凸五边形,凸五边形的话求周长面积
if(x.charAt(0)=='2') {
String[] str=x.split(" ");
String[] str1=str[0].split(",");
String[] str2=str[1].split(",");
String[] str3=str[2].split(",");
String[] str4=str[3].split(",");
String[] str5=str[4].split(",");
String[] str_=str1[0].split(":");
double x1=Double.parseDouble(str_[1]);
double y1=Double.parseDouble(str1[1]);
double x2=Double.parseDouble(str2[0]);
double y2=Double.parseDouble(str2[1]);
double x3=Double.parseDouble(str3[0]);
double y3=Double.parseDouble(str3[1]);
double x4=Double.parseDouble(str4[0]);
double y4=Double.parseDouble(str4[1]);
double x5=Double.parseDouble(str5[0]);
double y5=Double.parseDouble(str5[1]);
if(m!=5)
System.out.print("wrong number of points");
else {
double a,b,c,s=0,n=0,z=0;
int l=0,r=0;
a=y1-y2;
b=x1-x2;
c=(x1-x2)*y2-x1*(y1-y2);
s=a*x3+b*y3+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
s=a*x4+b*y4+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
s=a*x5+b*y5+c;
if(s>0)
l++;
else if(s<0)
r++;
else n++;
if(l==3||r==3)
z++;
//.2
int l2=0,r2=0;
a=y2-y3;
b=x2-x3;
c=(x2-x3)*y3-x2*(y2-y3);
s=a*x1+b*y1+c;
if(s>0)
l2++;
else if(s<0)
r2++;
else n++;
s=a*x4+b*y4+c;
if(s>0)
l2++;
else if(s<0)
r2++;
else n++;
s=a*x5+b*y5+c;
if(s>0)
l2++;
else if(s<0)
r2++;
else n++;
if(l==3||r==3)
z++;
//.3
int l3=0,r3=0;
a=y3-y4;
b=x3-x4;
c=(x3-x4)*y4-x3*(y3-y4);
s=a*x1+b*y1+c;
if(s>0)
l3++;
else if(s<0)
r3++;
else n++;
s=a*x2+b*y2+c;
if(s>0)
l3++;
else if(s<0)
r3++;
else n++;
s=a*x5+b*y5+c;
if(s>0)
l3++;
else if(s<0)
r3++;
else n++;
if(l==3||r==3)
z++;
//.4
int l4=0,r4=0;
a=y4-y5;
b=x4-x5;
c=(x4-x5)*y5-x4*(y4-y5);
s=a*x1+b*y1+c;
if(s>0)
l4++;
else if(s<0)
r4++;
else n++;
s=a*x2+b*y2+c;
if(s>0)
l4++;
else if(s<0)
r4++;
else n++;
s=a*x3+b*y3+c;
if(s>0)
l4++;
else if(s<0)
r4++;
else n++;
if(l==3||r==3)
z++;
//.5
int l5=0,r5=0;
a=y5-y1;
b=x5-x1;
c=(x5-x1)*y1-x5*(y5-y1);
s=a*x2+b*y2+c;
if(s>0)
l5++;
else if(s<0)
r5++;
else n++;
s=a*x3+b*y3+c;
if(s>0)
l5++;
else if(s<0)
r5++;
else n++;
s=a*x4+b*y4+c;
if(s>0)
l5++;
else if(s<0)
r5++;
else n++;
if(l==3||r==3)
z++;
if(n!=15)
System.out.print("not a pentagon");
else {
if(z==5) {
double zc=0,mj=0;
zc=Math.pow((Math.pow((x2-x1),2)+Math.pow((y2-y1),2)),0.5)+
Math.pow((Math.pow((x3-x2),2)+Math.pow((y3-y2),2)),0.5)+
Math.pow((Math.pow((x4-x3),2)+Math.pow((y4-y3),2)),0.5)+
Math.pow((Math.pow((x5-x4),2)+Math.pow((y5-y4),2)),0.5)+
Math.pow((Math.pow((x1-x5),2)+Math.pow((y1-y5),2)),0.5);
mj=x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2+
x1*y4-x1*y5+x4*y5-x4*y1+x5*y1-x4*y4+
x1*y3-x1*y4+x3*y4-x3*y1+x4*y1-x3*y3;
System.out.print("true ");
System.out.printf("%.3f",zc);
System.out.printf("%.3f",mj);
}
else
System.out.print("false");
}
}
}
//3.七点坐标,交点数量
if(x.charAt(0)=='3') {
String[] str=x.split(" ");
String[] str1=str[0].split(",");
String[] str2=str[1].split(",");
String[] str3=str[2].split(",");
String[] str4=str[3].split(",");
String[] str5=str[4].split(",");
String[] str6=str[4].split(",");
String[] str7=str[4].split(",");
String[] str_=str1[0].split(":");
double x1=Double.parseDouble(str_[1]);
double y1=Double.parseDouble(str1[1]);
double x2=Double.parseDouble(str2[0]);
double y2=Double.parseDouble(str2[1]);
double x3=Double.parseDouble(str3[0]);
double y3=Double.parseDouble(str3[1]);
double x4=Double.parseDouble(str4[0]);
double y4=Double.parseDouble(str4[1]);
double x5=Double.parseDouble(str5[0]);
double y5=Double.parseDouble(str5[1]);
double x6=Double.parseDouble(str6[0]);
double y6=Double.parseDouble(str6[1]);
double x7=Double.parseDouble(str7[0]);
double y7=Double.parseDouble(str7[1]);
if(m!=7)
System.out.print("wrong number of points");
else if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x1==x4)&&(y1==y4))||
((x2==x3)&&(y2==y3))||((x2==x4)&&(y2==y4))||((x3==x4)&&(y3==y4)))
System.out.print("points coincide");
}
}
}
}`
在创建类的时候,一定要注意返回值,这里m是int类型,创建的类也应是int类型,不然就只会显示

也不能使用void类型,在这里是需要返回数值的,而且是需要传形参的,String x必不可少需要代入所输入的数据才能进行操作
int m=shu(x); public static int shu(String x) { int i,m=0; for(i=0;i<x.length();i++){ if(x.charAt(i) == ','){ m++; } } return m; }
代码改进的话,这次的正则表达式没有再像上次那样一个一个去对正,而是
if(x.matches("^[1-5]?:([+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)? ?)+$")!=true)
这样写看起来也更简洁,写起来也更方便。
总结:
这次感触最深的还是自己数学不行,导致写代码的思路都没有,四边形五边形光是数学题目就不会做,何况是写代码,还有写代码时不能太粗心了,像这次的期中考试这种低级错误本不该出现,但我不仅犯错了,还在多次检查后未能发现错误。做题过程中也让我明白,代码世界还很大,等着我们探索,每一道题都是外来的挑战,只能通过不断挑战去解决问题。值得说的是,在室友的耐心教导下,学会了调试,现在还没感觉到有什么用,但是以后肯定会用的上的,四边形五边形,以后说不定就是六边形七边形八边形,里面的数学思维肯定需要更加严谨,所以在学专业课的同时,还得普及专业外的知识,才能在这条路上走下去。
浙公网安备 33010602011771号