问题描述
packagelianxi;publicclassLine{Pointstartpoint;Pointendpoint;publicvoidPoint(doublex1,doubley1,doublex2,doubley2){startpoint=newPoint(x1,y1);endpoint=newPoint(x2,y2);}publicvoidLine(Pointstartpoint,Pointendpoint){this.startpoint=startpoint;this.endpoint=endpoint;}publicdoublegetLength(){returnMath.sqrt(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubPointp1=newPoint(5,25);Pointp2=newPoint(10,10);Lineline=newLine();System.out.println(line.getLength());}}
解决方案
解决方案二:
你Line的构造方法都写错啦,没有voidpublicclassLine{staticclassPoint{doublex;doubley;Point(doublex,doubley){this.x=x;this.y=y;}}Pointstartpoint;Pointendpoint;publicLine(doublex1,doubley1,doublex2,doubley2){startpoint=newPoint(x1,y1);endpoint=newPoint(x2,y2);}publicLine(Pointstartpoint,Pointendpoint){this.startpoint=startpoint;this.endpoint=endpoint;}publicdoublegetLength(){returnMath.sqrt(Math.pow(startpoint.x-endpoint.x,2)+Math.pow(startpoint.y-endpoint.y,2));}publicstaticvoidmain(String[]args){Pointp1=newPoint(5,25);Pointp2=newPoint(10,20);Lineline=newLine(p1,p2);System.out.println(line.getLength());Lineline2=newLine(5,25,10,10);System.out.println(line2.getLength());}}
解决方案三:
importjava.util.Scanner;publicclassDistance{/***已知两点的坐标位置求二者之间的直线距离*@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubDistancedist=newDistance();Scannerscan=newScanner(System.in);System.out.println("请输入第一个点的横坐标x");doublex1=scan.nextDouble();System.out.println("请输入第一个点的横坐标y");doubley1=scan.nextDouble();System.out.println("请输入第二个点的横坐标x");doublex2=scan.nextDouble();System.out.println("请输入第二个点的横坐标x");doubley2=scan.nextDouble();System.out.println("两者之间的距离是:"+dist.getLength(x1,x2,y1,y2));}publicstaticdoublegetLength(doublex1,doublex2,doubley1,doubley2){returnMath.sqrt(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));}}
解决方案四:
importjava.util.Scanner;publicclassDistance{/***已知两点的坐标位置求二者之间的直线距离*@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubPointp1=newPoint(5,25);Pointp2=newPoint(10,10);System.out.println("两者之间的距离是:"+getLength(p1,p2));}publicstaticdoublegetLength(Pointp1,Pointp2){System.out.println(p1.x);System.out.println(p2.x);System.out.println(p1.y);System.out.println(p2.y);returnMath.sqrt(((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));}}classPoint{doublex;doubley;publicPoint(doublex,doubley){this.x=x;this.y=y;}}
解决方案五:
你的Line的构造方法应该把void去掉,同时建议你自己再建一个Point类,把你这个类里面的point方法Point类里面去,当做一个有参构造,然后再再这个Line里面定义两个Point类型的变量,利用构造方法赋值进去,然后再调用getLength()方法。
解决方案六:
建议去超市买一把尺子直接量,距离远一点的就使用卫星测距,(^o^)/~
解决方案七:
packagecom.company;/***@authorYang*CreatedbyScott_Wangon30/09/2014.*//**classPointinclude(x,y)positionforeachpoint**/classPoint{privatedoublex;privatedoubley;//pointconstructorPoint(doublex,doubley){this.x=x;this.y=y;}publicdoublegetX(){returnx;}publicdoublegetY(){returny;}}/**Lineclass*/publicclassLine{//createtwopointreferencesprivatePointstartPoint,endPoint;privatedoublelength;//staticmethodtoget(a-b)*(a-b)staticdoublesq(doublex1,doublex2){return(x1-x2)*(x1-x2);}Line(intx1,inty1,intx2,inty2){//initiatetwopointsinlineclassconstructorstartPoint=newPoint(x1,y1);endPoint=newPoint(x2,y2);length=0.0;//getdistancelength=dis(startPoint,endPoint);}//distancebetweentwopointspublicstaticdoubledis(Pointp1,Pointp2){returnMath.sqrt((sq(p1.getX(),p2.getX())+sq(p1.getY(),p2.getY())));}//-------fortest----------publicstaticvoidmain(String[]args){Lineline=newLine(5,25,10,10);System.out.println(line.length);}}
解决方案八:
希望对您有帮助