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)
请问这是什么原因?怎么才能得到正确的结果?请高手指教

解决方案 »

  1.   

    if((p1==null)||(p2==null)); //这句的分号去掉
      

  2.   

    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);
    yNew = (p1.y / 2.0) + (p2.y / 2.0);
    return new Location(xNew, yNew); }
    }
    主要问题是return语句的位置和static的使用,好好理解下static
      

  3.   

    另外给Location类增加个toString()方法,看的清晰点:
    public String toString(){
    return "Point:("+x+","+y+")";
    }
      

  4.   

    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);
            Location.distance(l1,l2);
            //System.out.println(Location.);
            Location l3=Location.midpoint(l1,l2);
       }}
    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;
    System.out.println("the distance is"+Math.sqrt(a*a+b*b));
    return 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);
    System.out.println("the position is ("+xNew+","+yNew);
        return new Location(xNew,yNew);

    }
    }