当前位置: 代码迷 >> J2SE >> 请高手指教!基础知识有关问题,多谢!
  详细解决方案

请高手指教!基础知识有关问题,多谢!

热度:101   发布时间:2016-04-24 16:24:59.0
请高手指教!基础知识问题,谢谢!!
public   class   LocationTest   {

/**
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
Location   l1=new   Location(1.0,1.0);
Location   l2=new   Location(0.0,0.0);
//Location.distance(l1,l2);
                System.out.println(Location.distance(l1,l2));
                //System.out.println(Location.);
                Location   l3=Location.midpoint(l1,l2);
                String   str=l3.toString();
                System.out.println( "它们的中点是: "+str);
}

}
class   Location
{
private   static   double   x,y;
public   Location(double   x,double   y)
{
this.x=x;
this.y=y;
}
public   static   double   distance(Location   p1,Location   p2)
{
double   a,b;
if((p1==null)||(p2==null))
return   Double.NaN;

a=p1.x-p2.x;
b=p1.y-p2.y;
return   Math.sqrt(a*a+b*b);
//System.out.println((double)Math.sqrt(a*a+b*b));
}
public   static   Location   midpoint(Location   p1,Location   p2)
{
double   xNew=0,yNew=0;
if((p1==null)||(p2==null));
return   null;
xNew=(p1.x/2.0)+(p2.x/2.0);
yNew=(p1.y/2.0)+(p2.y/2.0);
return   new   Location(xNew,yNew);

}
}
我写的这个程序本意是求两个点之间的距离以及中点坐标,但输出结果是错误的。另外“ xNew=(p1.x/2.0)+(p2.x/2.0);
yNew=(p1.y/2.0)+(p2.y/2.0);”
这两句提示:
Exception   in   thread   "main "   java.lang.Error:   Unresolved   compilation   problem:  
Unreachable   code

at   com.blackstars.Location.midpoint(LocationTest.java:45)
at   com.blackstars.LocationTest.main(LocationTest.java:15)
请问这是什么原因?怎么才能得到正确的结果?请高手指教。。。

------解决方案--------------------
if((p1==null)||(p2==null)); //这句的分号去掉
------解决方案--------------------
package net.rubyeye.javabean;

public class LocationTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Location l1 = new Location(1.0, 1.0);
Location l2 = new Location(0.0, 0.0);
// Location.distance(l1,l2);
System.out.println(Location.distance(l1, l2));
// System.out.println(Location.);
Location l3 = Location.midpoint(l1, l2);
String str = l3.toString();
System.out.println( "它们的中点是: " + str);
}

}

class Location {
private double x, y;

public Location(double x, double y) {
this.x = x;
this.y = y;
}

public static double distance(Location p1, Location p2) {
double a, b;
if ((p1 == null) || (p2 == null))
return Double.NaN;

a = p1.x - p2.x;
b = p1.y - p2.y;
return Math.sqrt(a * a + b * b);
// System.out.println((double)Math.sqrt(a*a+b*b));
}

public static Location midpoint(Location p1, Location p2) {
double xNew = 0, yNew = 0;
if ((p1 == null) || (p2 == null))

return null;
xNew = (p1.x / 2.0) + (p2.x / 2.0);
  相关解决方案