用JAVA编写 求平面两点的距离应该怎么写?好象是要定义两个类的是吧?会的请指教了,谢谢。

解决方案 »

  1.   

    sqrt((p1.x-p2.x)*(p1.x-p2.X)+(p1.y-p2.y)*(p1.y-p2.y))
      

  2.   

    package csdn;
    import java.math.*;
    public class Point { /**
     * @param args
     */
    int x,y;
    Point(){

    }
    Point(int x,int y){
    this.x=x;
    this.y=y;
    }

    double distance(Point a,Point b){
    double dist=0;
    dist = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

    return dist;
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Point p1 = new Point(1,1);
    Point p2 = new Point(1,1);
    System.out.println("Distance is :"+new Point().distance(p1,p2));
    }}
      

  3.   


    import java.math.*;
    public class Point { int x,y;
            Point(){

    }
     Point(int x,int y){
    this.x=x;
    this.y=y;
    }

    double distance( Point a, Point b){
    double dist=0;
    dist = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

    return dist;
    }
    public static void main(String[] args) {

     Point p1 = new Point(4,15);
     Point p2= new Point(2,18);
     System.out.println("Distance is :"+new Point().distance(p1,p2));
    }}这样写好象有不少错误咯,晕死
      

  4.   

    Distance is :3.605551275463989
    没有问题吧? 
    你怎么运行的?
      

  5.   

    我运行后显示这样的东西:C:\Java>java point
    Exception in thread "main" java.lang.NoClassDefFoundError: point (wrong name: c
    dn/Point)
      

  6.   

    import java.math.*;
    public class Point { int x,y;
            Point(){

    }
     Point(int x,int y){
    this.x=x;
    this.y=y;
    }

    double distance( Point a, Point b){
    double dist=0;
    dist = Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

    return dist;
    }
    public static void main(String[] args) {

     Point p1 = new Point(4,15);
     Point p2= new Point(2,18);
     System.out.println("Distance is :"+new Point().distance(p1,p2));
    }}我像这样编写还是有这样一个错误C:\Java>javac point.java
    point.java:2: class Point is public, should be declared in a file named Point.ja
    va
    public class Point {
           ^
    1 error
      

  7.   

    C:\Java>javac Point.java
    类名大写的