当前位置: 代码迷 >> Eclipse >> 【初学者】程序异常,求大师指点,不甚感激
  详细解决方案

【初学者】程序异常,求大师指点,不甚感激

热度:67   发布时间:2016-04-23 11:56:02.0
【菜鸟求助】程序错误,求大师指点,不甚感激。
新手学习Java,按照书上所说写了如下程序,目的是求两个点之间的距离。

public class point 
 { public double x,y;
  public double distance(point that)
  {double xdiff,ydiff;
  xdiff=x-that.x;
  ydiff=y-that.y;
  return Math.sqrt(xdiff*xdiff+ydiff*ydiff);
  }
point point1=new point();
point point2=new point();
point1.x=0.0;
point1.y=0.0;
point2.x=5.0;
point2.y=10.0;
d=point1.distance(point2);
System.out.println(d);
 }

编译时提示倒数第3行至倒数第六行的x和y有错误。

------解决方案--------------------
你贴出来的是point类的完整代码吗?如果是的,你见过把代码块直接写类里面的吗?

point point1=new point();
 point point2=new point();
 point1.x=0.0;
 point1.y=0.0;
 point2.x=5.0;
 point2.y=10.0;
 d=point1.distance(point2);
 System.out.println(d);

把这一块放方法里封装起来,还有你的变量d没有定义。

java不允许在类里面直接写代码的
------解决方案--------------------
class point {
public double x,y;
public double distance(point that)
{double xdiff,ydiff;
xdiff=x-that.x;
ydiff=y-that.y;
return Math.sqrt(xdiff*xdiff+ydiff*ydiff);
}
}
public class yy{
public static void main(String[] args){
double d;
 point point1=new point();
 point point2=new point();
 point1.x=0.0;
 point1.y=0.0;
 point2.x=5.0;
 point2.y=10.0;
 d=point1.distance(point2);
 System.out.println(d);
}
}

帮你改了
  相关解决方案