一.介紹
JavaAP在javafx.geometry包中有一個便于使用的Point2D類,用于表示二維平面上的點,
可以為給定x和y坐標的點來創建一個Point2D物件,使用distance方法計算該點到另外一個點之間的距離,并且使用toStringO方法來回傳該點的字串表示,
二.代碼
package com.zhuo.demo;
import javafx.geometry.Point2D;
import java.util.Scanner;
public class TestPoint2D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("請輸入點1的坐標: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("請輸入點2的坐標: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point2D p1 = new Point2D(x1, y1);
Point2D p2 = new Point2D(x2, y2);
System.out.println("點1的坐標為: " + p1.toString());
System.out.println("點2的坐標為: " + p2.toString());
System.out.println("點1到點2的距離為: " + p1.distance(p2));
}
}
三.運行結果
請輸入點1的坐標: 1.5 5.5
請輸入點2的坐標: -5.3 -4.4
點1的坐標為: Point2D [x = 1.5, y = 5.5]
點2的坐標為: Point2D [x = -5.3, y = -4.4]
點1到點2的距離為: 12.010412149464313
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/260398.html
標籤:java
上一篇:Java基礎-final關鍵字
