【问题标题】:Java methods getting euclidean distance获取欧几里得距离的Java方法
【发布时间】:2014-11-28 03:42:32
【问题描述】:
public class Point
{ 
// Placeholders for xcoordinate, ycoordinate, and quadrants
int xcoord = 0;
int ycoord =0;
double distance = 0.0;
String quadrant = ("NW");


//moveUp changes the y coordinate 
void moveUp (int x) {
    int moveUp = ycoord + x;
    ycoord= moveUp;
    System.out.println(moveUp);
    }
// moveDown changes the y coordinate    
void moveDown (int y){
    int moveDown = ycoord - y;
    ycoord =moveDown;
    System.out.println(moveDown);}
// moveLeft changes the x coordinate    
void moveLeft (int f){
    int moveLeft = xcoord -f ;
    xcoord = moveLeft;
    System.out.println(moveLeft);}
// moveRight changes the x coordinate   
void moveRight (int h) {
    int moveRight  = xcoord +h ;
    xcoord = moveRight;
    System.out.println(moveRight);}

//  This takes the y coordinate and the xcoordinate and places it into a quadrant. Then it returns the quadrant.
String quadrant () {    
    if (xcoord >= 0 && ycoord  >=0){
        return quadrant = ("NE"); }
    else if ( xcoord < 0 && ycoord < 0 ) {
        return quadrant = ("SW");}
    else if (xcoord <0 && ycoord >0 ){
        return quadrant = ("NW");}
    else if (xcoord >0 && ycoord <0){
        return quadrant = ("SE");}
    else {
        return quadrant = ("Origin");}  
}
//euclidean distance (?)
Point distance (Point other) {
    Point result = new Point(); 
    result.ycoord = Math.abs (ycoord - other.ycoord);
    result.xcoord = Math.abs (xcoord- other.xcoord);    
    result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
    System.out.println(result);
    return result;

    }

}   

这是整个代码。我遇到的问题是由于某种原因我的距离方法无法正常工作,我不知道为什么。它返回Point@24a42c89,而不是任何数字。请让我知道为什么它返回这个而不是一个数字。非常感谢。我是初学者,对 Java 知之甚少。

【问题讨论】:

    标签: java methods euclidean-distance


    【解决方案1】:

    你得到一个Point 作为结果,因为你返回一个Point 对象。在计算两点之间的距离时,没有理由声明 Point 对象。这里所有计算的结果都是数字,所以将所有变量声明为double

    double distance (Point other) {
        double deltaX = ycoord - other.ycoord;
        double deltaY = xcoord - other.xcoord;
        double result = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
        return result; 
    }
    

    请注意,不需要Math.abs(),因为将两个数字平方总是会产生非负数。

    这也消除了在每个点存储distance 值的需要。这应该是有道理的,因为一个点是一对 (x, y) 坐标,但 有一个固有的距离。而“距离”是两点之间的关系。

    【讨论】:

      【解决方案2】:

      您的方法返回一个 Point,因为您返回 result,它是 Point 的一个实例。如果您想要距离的双倍(值),您应该这样做:

      //euclidean distance (?)
      double distance (Point other) {
          Point result = new Point(); 
          result.ycoord = Math.abs (ycoord - other.ycoord);
          result.xcoord = Math.abs (xcoord- other.xcoord);    
          result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
          System.out.println(result);
          return result.distance; 
          }
      } 
      

      【讨论】:

      • 完全摆脱Point result并将所有计算存储为doubles。
      • 返回类型是Point所以我觉得有必要
      • @KickButtowski 觉得有什么必要吗?
      • @Code-Apprentice 我完全同意你的看法,但接受什么取决于操作员:)
      【解决方案3】:

      在你的距离函数

      Point distance (Point other) {
           ...
      
           rest of your code 
           ...
          System.out.println(result); <-- you have tired to print out the object
          return result;
      
          }
      

      要纠正您的问题,您需要打印出您正在寻找的对象的字段distance

       System.out.println(result.distance);
      

      一个建议

      您可以将返回类型 distance 函数从 Point 更改为 int

      为什么?

      因为返回的对象Point不代表距离的值

      【讨论】:

      • 我不确定你的意思。你能进一步解释一下吗?
      • 好的!这更有意义!感谢您的帮助!
      猜你喜欢
      • 2013-03-02
      • 2015-07-15
      • 2014-02-04
      • 1970-01-01
      • 2013-04-07
      • 2021-10-01
      • 2012-12-21
      相关资源
      最近更新 更多