【发布时间】: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