【发布时间】:2021-04-15 19:06:59
【问题描述】:
这是我的代码。我已经写了这个方法返回形状的优势点。最小,最大 2 分(左低,右上)。但是我的返回值有错误,请在此处查看我的方法:
@Override
public Point2D[] getPoints() {
Point2D[] ans = new Point2D[2];
if (this._point1.getX()<=this._point2.getX()) {
ans[0] =new Point2D(this._point1);
ans[1] =new Point2D(this._point2);
}
else{
ans[0] =new Point2D(this._point2);
ans[1] =new Point2D(this._point1);
}
return ans;
}
这是Point2D的课程:
public class Point2D implements GeoShape{
public static final double EPS1 = 0.001, EPS2 = Math.pow(EPS1,2), EPS=EPS2;
public static final Point2D ORIGIN = new Point2D(0,0);
private double _x,_y;
public Point2D(double x,double y) {
_x=x; _y=y;
}
public Point2D(Point2D p) {
this(p.x(), p.y());
}
public Point2D(String s) {
try {
String[] a = s.split(",");
_x = Double.parseDouble(a[0]);
_y = Double.parseDouble(a[1]);
}
catch(IllegalArgumentException e) {
System.err.println("ERR: got wrong format string for Point2D init, got:"+s+" should be of format: x,y");
throw(e);
}
}
public double x() {return _x;}
public double y() {return _y;}
public double getX() {return _x;}
public double getY() {return _y;}
public int ix() {return (int)_x;}
public int iy() {return (int)_y;}
public Point2D add(Point2D p) {
Point2D a = new Point2D(p.x()+x(),p.y()+y());
return a;
}
@Override
public String toString()
{
return _x+","+_y;
}
public double distance()
{
return this.distance(ORIGIN);
}
public double distance(Point2D p2)
{
double dx = this.x() - p2.x();
double dy = this.y() - p2.y();
double t = (dx*dx+dy*dy);
return Math.sqrt(t);
}
此方法getpoints 的返回有错误。检查此图像:
返回类型与GeoShape.getPoints()不兼容。
有人知道是什么原因造成的吗?
【问题讨论】:
-
GeoShape是一个接口,这个接口中getPoints方法的签名是什么? -
图片不工作
标签: java visual-studio-code error-handling syntax