首页 > 编程 > Java > 正文

java实现——2001计算两点间的距离

2019-11-06 06:28:13
字体:
来源:转载
供稿:网友
import java.text.DecimalFormat;import java.util.Scanner;public class Main {	PRivate double x;	private double y;			public Main(double x, double y) {		this.x = x;		this.y = y;	}	public double getDistance(Main p){		double _x = Math.abs(this.x-p.x);		double _y = Math.abs(this.y-p.y);		return Math.sqrt(_x*_x+_y*_y);	}	public static void main(String[] args) {		Scanner scan = new Scanner(System.in);		DecimalFormat df = new DecimalFormat("0.00");		while(true){			String a[] = scan.nextLine().split(" ");			double b[] = new double[4];			for(int i=0; i<a.length; ++i)			{				b[i] = Double.parseDouble(a[i]);			}			Main p1 = new Main(b[0],b[1]);			Main p2 = new Main(b[2],b[3]);			System.out.println(df.format(p1.getDistance(p2)));		}	}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表