java小练习,输入两点计算距离<类的使用>

计算两点距离

import java.util.Scanner;

public class Point {
    double x, y;

    public void get() {
        Scanner s = new Scanner(System.in);
        System.out.println("please input x:>");
        x = s.nextDouble();
        System.out.println("please input y:>");
        y = s.nextDouble();
    }

    public double dis(Point p) {
        return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    }

    public static void main(String[] args) {
        Point p1=new Point();
        p1.get();
        Point p2=new Point();
        p2.get();
        double distance=p2.dis(p1);
        System.out.println("distance is "+distance);
    }
}

 


版权声明:本文为zl295871597原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。