计算两点间的公式是 =
注意:可以使用Math.pow(a,0.5)来计算
![]()
下面是一个运算示例:
Enter x1 and y1: 1.5 -3.4
Enter x1 and y2: 4 5
The distance between the two points is 8.764131445842194
package Second;
import java.util.Scanner;
public class Code_15 {
public static void main(String[] args) {
System.out.print("Enter x1 and y1:");
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.println("Enter x1 and y2:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double a= Math.pow(x2 - x1, 2) + Math.pow(y2 - y1,2);
System.out.println("The distance between the two points is " + Math.pow(a,0.5));
}
}
输出
Enter x1 and y1:1.5 -3.4 Enter x1 and y2: 4 5 The distance between the two points is 8.764131445842194
这个博客展示了如何使用Java编程计算平面上两点之间的距离。程序通过获取用户输入的两个点的坐标,然后应用距离公式(平方根下(x2-x1)的平方加(y2-y1)的平方)来计算它们之间的距离。示例运行输出了8.764131445842194作为两点(1.5, -3.4)和(4, 5)之间的距离。
807

被折叠的 条评论
为什么被折叠?



