编程程序,提示用户输入两个点(x1,y1)、(x2,y2),然后显示两点之间的距离。使用Math.pow()
计算两点间的距离公式为
import java.util.Scanner;
public class zuoye2_14 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner reader=new Scanner(System.in);
double x1,x2,y1,y2,k1,k2;
System.out.print("Enter x1 and y1:");
x1=reader.nextDouble();
y1=reader.nextDouble();
System.out.print("Enter x2 and y2:");
x2=reader.nextDouble();
y2=reader.nextDouble();
k1=Math.pow((x2-x1), 2)+Math.pow((y2-y1), 2);
k2=Math.pow(k1, 0.5);
System.out.print("The distance between the two points is "+k2);
}
}
输出
输入
1.5 -3.4
4 5