javafx.geometry.Point2D
+Point2D(x: double,y: double) //用给定的x和y坐标来创建一个Point2D对象
+distance(x: double,y: double): double //返回该点到定点(x,y)的距离
+distance(p: Point2D): double //返回该点到p点的距离
+getX(): double //返回该点的x的坐标
+getY(): double //返回该点的y的坐标
+toString(): String //返回该点的字符串表示
import java.util.Scanner;
import javafx.geometry.Point2D;
public class TestPoint2D {
public static void main(String[] args){
Scanner input= new Scanner(System.in);
System.out.println("Enter point1's x-,y-coordinates:");
double x1=input.nextDouble();
double y1=input.nextDouble();
System.out.println("Enter point2's x-,y-coordinates:");
double x2=input.nextDouble();
double y2=input.nextDouble();
Point2D p1 =new Point2D(x1,y1);
Point2D p2 =new Point2D(x2,y2);
System.out.println("p1 is"+p1.toString());
System.out.println("p2 is"+p2.toString());
System.out.println("distance is"+p1.distance(p2));
}
}
复制代码
为什么会把这个类单拿出来呢,因为在eclipse运行的时候,你会发现会报错,难道是书写错了?不不不,下面是答案:
这是因为这个类是Eclipse的JRE带的,但并不属于JAVA的公开API。
解决方法:
右击项目->属性->Java Compiler->Errors/Warnings->Deprecated and restricted API->Forbidden reference -> Ignore
运行结果
Enter point1's x-,y-coordinates:
1 2
Enter point2's x-,y-coordinates:
3 4
p1 isPoint2D [x = 1.0, y = 2.0]
p2 isPoint2D [x = 3.0, y = 4.0]
distance is2.8284271247461903
复制代码