有人能告訴我我做錯了什麼嗎?我在代碼下面列出了錯誤。我只是用Java語言讓自己的腳步變得如此糟糕,請在你的迴應中表現出色。當我運行驅動一個點類的位置
//The Point class definition
public class Point
{
private int x;
private int y;
// Constructors
public Point()
{
x = 0;
y = 0;
}
public Point(int newX, int newY)
{
x = newX;
y = newY;
}
// Getters and Setters
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setX(int newX)
{
x = newX;
}
public void setY(int newY)
{
y = newY;
}
public double distance(Point another) //takes one parameter of Point type and returns a double
{
double xDiff = x - another.x;
double yDiff = y - another.y;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
public void translate(int dx, int dy)
//takes two int parameters for values used to translate,returns a new point with a new location (do not update current location) point.
{
x = x + dx;
y = y + dy;
}
public void setXY(int newX, int newY) //updates the location of the point all at one time.
{
x = newX;
y = newY;
}
public String toString()
{
return "(" + x + "," + y + ")";
}
}
錯誤:
PointDriver.java:24: error: method distance in class Point cannot
be applied to given types;
pt3 = pt2.distance(3, -10);
^ required: Point found: int,int reason: actual and formal argument lists differ in length
PointDriver.java:31: error: method distance in class Point cannot be
applied to given types;
pt1 = pt1.distance(4, -2);
^ required: Point
難道你不明白什麼部分錯誤消息的? – SLaks 2014-10-20 20:11:10
由於錯誤發生在'PointDriver'可能表明代碼會有好處?你是否應該定義一個方法'公共雙距離(int x,int y){}'? – clcto 2014-10-20 20:13:03
您已經定義了distance()方法來獲取Point參數,但試圖傳入兩個整數。 – BarrySW19 2014-10-20 20:14:17