Math.sqrt一种有效,为什么不呢?

时间:2018-09-08 07:07:24

标签: java

将以下方法添加到Point类:

公共距离(其他点)

返回当前Point对象与给定的另一个Point对象之间的距离。两点之间的距离等于它们的x坐标和y坐标之差的平方和的平方根。换句话说,两个点(x1,y1)和(x2,y2)之间的距离可以表示为(x2-x1)2 +(y2-y1)2的平方根。具有相同(x,y)坐标的两个点应该返回0.0的距离。

public class Point {
    int x;
    int y;

    // // your code goes here

}

这有效:

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

这不是:

public double distance(Point other){
    return Math.sqrt((this.x-other.x)^2 + (this.y-other.y)^2);
}

请问为什么? TY

1 个答案:

答案 0 :(得分:0)

要将其从未回答的问题列表中删除,因为我认为这不是错字:

^不是“授权”,而是XOR。