注册 登录
编程论坛 JAVA论坛

编写一个点类,利用方法输出两点的距离

sunshine萝卜 发布于 2016-04-22 13:19, 1924 次点击
public class Point {

    private double x,y;
    public Point(double a,double b){
        x = a;
        y = b;
    }
    public double distance(double x,double y){
        return Math.sqrt(x*x+y*y);
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Point One = new Point(1,1);
        Point Two = new Point(1,3);
        System.out.println("这个点到到原点距离为:" + );//我想在这里直接输出两点距离
    }
}
我这个程序应该存在问题,但是我不知道如何去修改
3 回复
#2
sunshine萝卜2016-04-22 13:21
引号里面打错了,应该是"两点间的距离为:"
#3
林月儿2016-04-22 19:21
程序代码:
public class Point {

    private double x,y;
    public Point(double a,double b){
        x = a;
        y = b;
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
    public double distance(Point p){
        return Math.sqrt(Math.pow(x-p.getX(),2)+Math.pow(y-p.getY(),2));
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Point One = new Point(1,1);
        Point Two = new Point(1,3);
        System.out.println("这个点到到原点距离为:" +One.distance(Two);//我想在这里直接输出两点距离
    }
}


[此贴子已经被作者于2016-4-22 19:22编辑过]

#4
sunshine萝卜2016-04-22 23:03
回复 3楼 林月儿
谢谢
1