计算两点间距离问题(我觉得没有错误,编译老是告诉我对象加点不能调用)

chdbj 2006-03-30 08:09:30
class Postion{
private int X; //定义变量
private int Y; //定义变量

Position(int x,int y){ //构造函数
X=x;
Y=y;
}

public int GetX(){
return X;
}
public int GetY(){
return Y;
}

public double distance(Position a, Position b){
double m;

m=Math.sqrt((a.GetX()-b.GetX())*(a.GetX()-b.GetX())+(a.GetY()-b.GetY())*
(a.GetY()-b.GetY()));
return m;
}
}

class Result{
public static void main(String args[]){
Postion source=new Postion(0,0);
Postion target=new Postion(5,8);

int x1,x2,y1,y2;
double dis;
x1=source.GetX();
y1=source.GetY();
x2=target.GetX();
y2=target.GetY();

System.out.println("source is x1="+x1+"source is y1="+y1);
System.out.println("target is x2="+x2+"target is y2="+y2);

dis=source.distance(source,target);

System.out.println("the distance is:"+dis);
}
}

//我觉得没有错误,编译老是告诉我对象加点不能调用,请大家帮忙看看,到底怎么回事?
...全文
160 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chdbj 2006-03-30
  • 打赏
  • 举报
回复
谢谢,感谢大家。 以后我用存取器给私有变量赋值了。
wizardblue 2006-03-30
  • 打赏
  • 举报
回复
楼主的变量命名不符合java规则
wizardblue 2006-03-30
  • 打赏
  • 举报
回复

public class Point {

private int x;

private int y;

public Point(int x, int y) { // 构造函数
this.x = x;
this.y = y;
}

/**
* @return Returns the x.
*/
public int getX() {
return x;
}

/**
* @param x
* The x to set.
*/
public void setX(int x) {
this.x = x;
}

/**
* @return Returns the y.
*/
public int getY() {
return y;
}

/**
* @param y
* The y to set.
*/
public void setY(int y) {
this.y = y;
}

public double distance(Point a, Point b) {
double offX = a.x - b.x;
double offY = a.y - b.y;

return Math.sqrt(offX * offX +offY * offY);

}

public static void main(String args[]) {
Point source = new Point(0, 0);
Point target = new Point(5, 8);

int x1, x2, y1, y2;
double dis;
x1 = source.getX();
y1 = source.getY();
x2 = target.getX();
y2 = target.getY();

System.out.println("source is x1=" + x1 + "source is y1=" + y1);
System.out.println("target is x2=" + x2 + "target is y2=" + y2);

dis = source.distance(source, target);

System.out.println("the distance is:" + dis);
}
}
chdbj 2006-03-30
  • 打赏
  • 举报
回复
我知道了,Position写错了,下次一定小心。
shenpipi 2006-03-30
  • 打赏
  • 举报
回复
老大,position定义的时候少了个字母i

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧