Java求两点距离(构造方法)

描述

输入两点坐标(X1,Y1)、(X2,Y2),运用构造方法,计算并输出两点间的距离。

输入

输入数据只有一组,占一行,由4个实数组成,分别表示x1,y1,x2,y2。数据之间用空格隔开。

输出

对于该组输入数据,输出占一行,结果保留两位小数。

难度

入门

输入示例

0 1 1 0

输出示例

The distance is 1.41
 
import java.util.Scanner;

public class javaTest {

    public static void main(String[] args) {
// TODO Auto-generated method stub
        double x1,y1,x2,y2;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()) {
            x1=sc.nextDouble();
            y1=sc.nextDouble();
            x2=sc.nextDouble();
            y2=sc.nextDouble();
            Point p1=new Point(x1,y1);
            Point p2=new Point(x2,y2);
            double d=dist(p1, p2);
            System.out.printf("The distance is %.2f\n", d);
        }
sc.close(); }
static double dist(Point p1,Point p2) { double s; double dx=Math.abs(p2.getX()-p1.getX()) ; double dy=Math.abs(p2.getY()-p1.getY()); s=Math.sqrt(dx*dx+dy*dy); return s; } } class Point { public double x,y; public Point(double a,double b){ x=a; y=b; } public double getX(){ return x; } public void setX(double x){ this.x=x; } public double getY(){ return y; } public void setY(double y){ this.y=y; } }

 

posted @ 2020-03-12 20:20  哦呦aholic  阅读(2253)  评论(0编辑  收藏  举报