繁体   English   中英

命令提示符中的不兼容类型错误

[英]Incompatible types error in Command Prompt

我有一项关于计算两点之间距离的工作。 它由点 Class、线 Class 以及主 Class 组成。 以下是我的观点class。 在处理私有双距离(点 p)方法后,我无法在公共双 getDistance(点 p)方法中返回 p。 我在命令提示符上运行代码,它显示错误:不兼容的类型:点不能转换为双精度。 请指教。

class Point
{
    private int x;
    private int y;

//Constructor
public Point()
{
    //nothing
}

//Second constructor
public Point (int x, int y)
{
    this.x = x;
    this.y = y;
}

//Copy constructor
public Point (Point p)
{
    this (p.x, p.y);
}

private double distance(Point p)
{
    int dX = this.x - p.x;
    int dY = this.y - p.y;
    double result = Math.sqrt(dX * dX + dY * dY);
    return result;
}

public double getDistance(Point p)
{        
    return p;
}

//getter
public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

//setter
public void set(int x, int y)
{
    this.x = x;
    this.y = y;
}

public String toString ()
{
    return String.format ("Given Point (%d, %d)", x, y);
}

}

您将 object 点p作为参数并将其作为双精度值返回。

在您的代码块中,您声明返回 Object 点 p 而不是双精度数据类型。

public double getDistance(Point p) {
   return p; 
}

如果您只是想计算 object 的距离,请使用您的distance()方法。 此方法已返回计算为double的距离。

private double distance(Point p) {
    int dX = this.x - p.x;
    int dY = this.y - p.y;
    double result = Math.sqrt(dX * dX + dY * dY);
    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM