2013-10-06 95 views
0

我想要獲得座標之間的距離,並且由於某種原因它似乎不起作用。感恩,如果有人能幫助!兩個座標之間的距離

輸出:

從A點到B點的距離爲0.0。點a 到點b的距離爲0.0。從點a到點b的距離爲0.0。從點a到點b的距離爲0.035。從P1到P2的距離 4.242640687119285從P1到P3的距離12.727922061357855

package GC01; 

public class Point { 
    private final double x; 
    private final double y; 
    private double distance; 

    public Point(){ 
     x=0.0; 
     y=0.0; 
    } 

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

    public double distanceTo(Point a, Point b) { 
     double dx = a.x - b.x; 
     double dy = a.y - b.y; 
     distance = Math.sqrt(dx*dx + dy*dy); 
     return distance; 
    } 
    public String toString(){ 
     return "The distance from Point a to Point b is " + distance +"."; 
    } 

public static void main(String[] args){ 
    Point p0 = new Point(); 
    Point p1 = new Point(0.0,0.0); 
    Point p2 = new Point(3.0,3.0); 
    Point p3 = new Point(9.0,9.0); 
    System.out.println(p0.toString()); 
    System.out.println(p1.toString()); 
    System.out.println(p2.toString()); 
    System.out.println(p3.toString()); 
    System.out.println("The distance from p1 to p2 is "+p1.distanceTo(p1,p2)); 
    System.out.println("The distance from p1 to p3 is "+p1.distanceTo(p1,p3)); 
} 
} 
+1

我沒有看到問題出在哪裏,而只是一個建議。對於你的distanceTo函數,你應該只需要1個點的參數。第一點將是你「進入」的點,即這個。第二點將是參數。 – Kindread

+0

其實問題是什麼?鑑於你如何輸出,你正在得到你應該的價值。從p1到p2的距離是4.24。而p1到p3是12.727,這就是你所得到的。正如Andrew_CS指出的那樣,您的困惑可能是您在計算之前輸出了緩存距離。 – Kindread

+0

如果回答這個問題,你應該接受一個答案 - 這樣它就不會被標記爲未解決。 –

回答

2

一兩件事,我看到的是,當你運行你的主,你調用Point.toString()方法四你做出積分後的時間。當你這樣做時,距離變量還沒有被設置,因爲distanceTo方法沒有被調用。

Point p0 = new Point(); 
Point p1 = new Point(0.0,0.0); 
Point p2 = new Point(3.0,3.0); 
Point p3 = new Point(9.0,9.0); 
System.out.println(p0.toString()); 
System.out.println(p1.toString()); 
System.out.println(p2.toString()); 
System.out.println(p3.toString()); 

當這些Point.toString調用發生,distanceTo方法還沒有被調用所以距離尚未設置任何這些點。

因爲您調用distanceTo方法,所以您會在最後兩行輸出數字。

0

這是否適合您?

public class Point { 
    private final double x; 
    private final double y; 

    public Point() { 
     x = 0.0; 
     y = 0.0; 
    } 

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

    public double distanceTo(Point other) { 
     double dx = other.x - this.x; 
     double dy = other.y - this.y; 
     double distance = Math.sqrt(dx * dx + dy * dy); 
     return distance; 
    } 

    public String toString() { 
     return x + "/" + y; 
    } 

    public static void main(String[] args) { 
     Point p0 = new Point(); 
     Point p1 = new Point(0.0, 0.0); 
     Point p2 = new Point(3.0, 3.0); 
     Point p3 = new Point(9.0, 9.0); 
     System.out.println(p0.toString()); 
     System.out.println(p1.toString()); 
     System.out.println(p2.toString()); 
     System.out.println(p3.toString()); 
     System.out 
       .println("The distance from p1 to p2 is " + p1.distanceTo(p2)); 
     System.out 
       .println("The distance from p1 to p3 is " + p1.distanceTo(p3)); 
    } 
} 

我從你的類刪除distance變量,因爲只能有一個點和彼此之間的距離;一個點本身沒有距離。

我也改變了distanceTo:當您撥打p1.distanceTo(p2)時,您將p2的參考號傳遞給p1。 p2現在在distanceTo方法中被稱爲other

寫作this.x只是一種更詳細的寫作方式,只是寫作x。我想表明這x變量屬於distanceTo方法(在這種情況下爲p1)的接收方。

最後我改變了toString,以便它打印你的點的X和Y座標。在Java實例變量,如除去distance總是以0初始化這就是爲什麼px.toString()總是打印

從點A到點B的距離爲0.0