104 距離計算
import java.util.Scanner; public class JPA04 { public static void main(String[] args) { try { double x1, x2, y1, y2; Scanner sc = new Scanner(System.in); // 輸入四個數字 x1, y1, x2, y2 x1 = sc.nextDouble(); y1 = sc.nextDouble(); x2 = sc.nextDouble(); y2 = sc.nextDouble(); // 計算兩點的歐式距離公式:√((x2 - x1)² + (y2 - y1)²) double distance = Math.sqrt(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2)); // 輸出四捨五入至小數點後四位 System.out.printf("%.4f",distance); } catch (Exception e) { System.out.print("error"); System.exit(0); } } }