0

我正在尝试获取坐标之间的距离,但由于某种原因它似乎不起作用。如果有人可以提供帮助,将不胜感激!

输出:

a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。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));
}
}
4

2 回答 2

2

我看到的一件事是,当您运行 main 时,您在得分后调用 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 方法,所以在最后两行输出了数字。

于 2013-10-06T21:55:53.363 回答
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)你传递了p2to的引用p1。现在otherdistanceTo方法中调用 p2。

写作this.x只是一种更冗长的写作方式而已x。我想说明这个x变量属于distanceTo方法的接收者(p1在这种情况下)。

最后,我进行了更改toString,以便打印您的点的 X 和 Y 坐标。在 Java 中,例如 remove 的实例变量distance总是用 0 初始化。这就是为什么px.toString()总是打印

a 点到 b 点的距离为 0.0

.

于 2013-10-06T22:02:09.397 回答